/**
 * Shared Constants for Demo Booking System
 *
 * Centralized configuration values used across the application
 * Prevents magic numbers and ensures consistency
 */

/**
 * Allowed meeting durations
 */
export const ALLOWED_DURATIONS: readonly string[] = [
  "30min",
  "1hr",
  "1.5hr",
  "2hr",
];
export type Duration = "30min" | "1hr" | "1.5hr" | "2hr";

/**
 * Booking validation rules
 */
export const BOOKING_RULES = {
  /**
   * Minimum hours in advance a booking must be made
   * Default: 12 hours
   */
  MIN_ADVANCE_HOURS: 12,

  /**
   * Maximum days in future a booking can be made
   * Default: 365 days (1 year)
   */
  MAX_ADVANCE_DAYS: 365,

  /**
   * Maximum length for special requirements field
   * Default: 500 characters
   */
  SPECIAL_REQUIREMENTS_MAX_LENGTH: 500,
} as const;

/**
 * Webhook configuration constants
 */
export const WEBHOOK_CONFIG = {
  /**
   * Maximum webhook retry attempts
   */
  MAX_RETRIES: 3,

  /**
   * Timeout for each webhook request (milliseconds)
   */
  TIMEOUT_MS: 5000,

  /**
   * Exponential backoff delays between retries (milliseconds)
   * [1s, 3s, 9s]
   */
  RETRY_DELAYS_MS: [1000, 3000, 9000] as const,
} as const;

/**
 * Security configuration constants
 */
export const SECURITY_CONFIG = {
  /**
   * Rate limit: bookings per hour per IP
   */
  RATE_LIMIT_PER_HOUR: 5,

  /**
   * reCAPTCHA minimum score threshold (0.0 - 1.0)
   * 0.5 is recommended for forms
   */
  RECAPTCHA_THRESHOLD: 0.5,

  /**
   * CSRF token length in bytes
   */
  CSRF_TOKEN_LENGTH: 32,

  /**
   * Email hash length for privacy-compliant logging
   */
  EMAIL_HASH_LENGTH: 8,
} as const;

/**
 * Pagination configuration constants
 * Prevents DoS attacks by limiting the number of records returned per request
 */
export const PAGINATION_CONFIG = {
  /**
   * Maximum number of records that can be requested in a single API call
   * Prevents DoS attacks by limiting database query size
   */
  MAX_LIMIT: 1000,

  /**
   * Default number of records to return when limit is not specified
   */
  DEFAULT_LIMIT: 50,

  /**
   * Minimum number of records (must be at least 1)
   */
  MIN_LIMIT: 1,
} as const;

/**
 * Safely clamps a pagination limit value between MIN_LIMIT and MAX_LIMIT
 * @param requestedLimit - The limit value requested by the client
 * @returns A safe limit value between MIN_LIMIT and MAX_LIMIT
 */
export function clampPaginationLimit(
  requestedLimit: number | string | null | undefined,
): number {
  const parsed =
    typeof requestedLimit === "string"
      ? parseInt(requestedLimit, 10)
      : requestedLimit;
  const limit = parsed || PAGINATION_CONFIG.DEFAULT_LIMIT;
  return Math.min(
    Math.max(PAGINATION_CONFIG.MIN_LIMIT, limit),
    PAGINATION_CONFIG.MAX_LIMIT,
  );
}
