/**
 * Mawidi Demo Scheduler - Configuration
 * Central configuration for working hours, booking rules, and integrations
 */

import type { SchedulerConfig, WeeklySchedule } from "../types/scheduler.types";

/**
 * Working hours schedule for all days of the week
 * Times are in 24-hour format (HH:mm)
 * Timezone: UTC+3 (GCC Standard Time)
 */
export const WORKING_HOURS: WeeklySchedule = {
  sunday: {
    start: "14:00",
    end: "19:00",
    timezone: "UTC+3",
  },
  monday: {
    start: "14:00",
    end: "19:00",
    timezone: "UTC+3",
  },
  tuesday: {
    start: "14:00",
    end: "19:00",
    timezone: "UTC+3",
  },
  wednesday: {
    start: "14:00",
    end: "19:00",
    timezone: "UTC+3",
  },
  thursday: {
    start: "14:00",
    end: "19:00",
    timezone: "UTC+3",
  },
  friday: {
    start: "07:00",
    end: "17:00",
    timezone: "UTC+3",
  },
  saturday: {
    start: "07:00",
    end: "17:00",
    timezone: "UTC+3",
  },
};

/**
 * Time slot interval in minutes
 * Determines the granularity of available time slots
 */
export const TIME_SLOT_INTERVAL = 30;

/**
 * Default timezone for the scheduler
 * All working hours are defined in this timezone
 */
export const DEFAULT_TIMEZONE = "Asia/Riyadh"; // UTC+3

/**
 * Complete scheduler configuration
 */
export const SCHEDULER_CONFIG: SchedulerConfig = {
  workingHours: WORKING_HOURS,

  /**
   * Available meeting durations in minutes
   * Users can choose between 30-minute or 60-minute demos
   */
  meetingDurations: [30, 60],

  /**
   * Minimum advance booking requirement
   * Users must book at least 48 hours (2 days) in advance
   */
  minAdvanceBookingHours: 48,

  /**
   * Maximum advance booking window
   * Users can book up to 6 months (180 days) in advance
   */
  maxAdvanceBookingDays: 180,

  /**
   * n8n webhook URL for booking submissions
   * In production, this should be set via environment variable
   * In demo mode, submissions are logged but not sent
   */
  n8nWebhookUrl: process.env.NEXT_PUBLIC_N8N_WEBHOOK_URL || "",

  /**
   * Demo mode flag
   * When true, bookings are simulated and not actually sent to n8n
   * Useful for testing and development
   */
  demoMode: process.env.NEXT_PUBLIC_DEMO_MODE === "true",

  /**
   * Default timezone
   */
  timezone: DEFAULT_TIMEZONE,

  /**
   * Business hours (alias for workingHours)
   */
  businessHours: WORKING_HOURS,

  /**
   * Time slot interval in minutes
   */
  slotInterval: TIME_SLOT_INTERVAL,
};

/**
 * Supported timezones for display
 */
export const SUPPORTED_TIMEZONES = [
  "Asia/Riyadh", // Saudi Arabia (UTC+3)
  "Asia/Dubai", // UAE (UTC+4)
  "Asia/Kuwait", // Kuwait (UTC+3)
  "Asia/Bahrain", // Bahrain (UTC+3)
  "Asia/Qatar", // Qatar (UTC+3)
  "Asia/Muscat", // Oman (UTC+4)
] as const;

/**
 * LocalStorage keys for persistence
 */
export const STORAGE_KEYS = {
  BOOKING_DRAFT: "mawidi_booking_draft",
  USER_TIMEZONE: "mawidi_user_timezone",
  LAST_VISIT: "mawidi_last_visit",
} as const;

/**
 * Draft expiration time in milliseconds
 * Drafts older than 24 hours are discarded
 */
export const DRAFT_EXPIRATION_MS = 24 * 60 * 60 * 1000; // 24 hours

/**
 * API timeout configuration in milliseconds
 */
export const API_TIMEOUTS = {
  /** Timeout for n8n webhook requests */
  WEBHOOK: 15000, // 15 seconds

  /** Timeout for Outlook API requests */
  OUTLOOK: 10000, // 10 seconds

  /** Maximum retry attempts */
  MAX_RETRIES: 3,

  /** Delay between retries in milliseconds */
  RETRY_DELAY: 1000, // 1 second
} as const;

/**
 * UI text translations
 * Organized by language (en/ar)
 */
export const UI_TEXT = {
  en: {
    title: "Book a Demo",
    subtitle: "Schedule a personalized demonstration of Mawidi",
    steps: {
      duration: "Select Duration",
      date: "Choose Date",
      time: "Pick Time",
      details: "Your Details",
      confirm: "Confirm",
      success: "Confirmed",
    },
    durations: {
      30: "30 Minutes",
      60: "60 Minutes (Recommended)",
    },
    form: {
      name: "Full Name",
      namePlaceholder: "Enter your full name",
      email: "Email Address",
      emailPlaceholder: "your.email@example.com",
      phone: "Phone Number (Optional)",
      phonePlaceholder: "+966 XX XXX XXXX",
      message: "Message or Special Requirements",
      messagePlaceholder:
        "Tell us about your clinic and what you'd like to see...",
    },
    buttons: {
      next: "Next",
      back: "Back",
      confirm: "Confirm Booking",
      bookAnother: "Book Another Demo",
      cancel: "Cancel",
    },
    messages: {
      loading: "Loading available times...",
      submitting: "Confirming your booking...",
      success: "Your demo has been confirmed!",
      successDetails:
        "You will receive a confirmation email shortly with meeting details.",
      noSlotsAvailable:
        "No time slots available for this date. Please choose another date.",
      errorGeneric: "Something went wrong. Please try again.",
      errorNetwork:
        "Network error. Please check your connection and try again.",
      errorWebhook: "Unable to process booking. Please contact support.",
      minAdvanceWarning: "Please book at least 2 days (48 hours) in advance.",
    },
    validation: {
      nameRequired: "Name is required",
      nameMin: "Name must be at least 2 characters",
      emailRequired: "Email is required",
      emailInvalid: "Please enter a valid email address",
      phoneInvalid: "Please enter a valid phone number",
      messageRequired: "Message is required",
      messageMin: "Message must be at least 10 characters",
    },
    calendar: {
      today: "Today",
      tomorrow: "Tomorrow",
      selectDate: "Select a date",
      noAvailableDates: "No dates available in the next 6 months",
    },
    timezone: {
      label: "Your timezone",
      autoDetected: "Auto-detected",
    },
  },
  ar: {
    title: "احجز عرضًا توضيحيًا",
    subtitle: "احجز عرضًا توضيحيًا مخصصًا لنظام ماويدي",
    steps: {
      duration: "اختر المدة",
      date: "اختر التاريخ",
      time: "اختر الوقت",
      details: "بياناتك",
      confirm: "تأكيد",
      success: "مؤكد",
    },
    durations: {
      30: "٣٠ دقيقة",
      60: "٦٠ دقيقة (موصى به)",
    },
    form: {
      name: "الاسم الكامل",
      namePlaceholder: "أدخل اسمك الكامل",
      email: "البريد الإلكتروني",
      emailPlaceholder: "your.email@example.com",
      phone: "رقم الهاتف (اختياري)",
      phonePlaceholder: "٠٥٠٠ ٠٠٠ ٠٠٠",
      message: "رسالة أو متطلبات خاصة",
      messagePlaceholder: "أخبرنا عن عيادتك وما تريد رؤيته...",
    },
    buttons: {
      next: "التالي",
      back: "رجوع",
      confirm: "تأكيد الحجز",
      bookAnother: "احجز عرضًا آخر",
      cancel: "إلغاء",
    },
    messages: {
      loading: "جاري تحميل الأوقات المتاحة...",
      submitting: "جاري تأكيد حجزك...",
      success: "تم تأكيد موعد العرض التوضيحي!",
      successDetails:
        "سوف تتلقى بريدًا إلكترونيًا للتأكيد قريبًا مع تفاصيل الاجتماع.",
      noSlotsAvailable:
        "لا توجد فترات زمنية متاحة لهذا التاريخ. يرجى اختيار تاريخ آخر.",
      errorGeneric: "حدث خطأ ما. يرجى المحاولة مرة أخرى.",
      errorNetwork: "خطأ في الشبكة. يرجى التحقق من اتصالك والمحاولة مرة أخرى.",
      errorWebhook: "تعذر معالجة الحجز. يرجى الاتصال بالدعم.",
      minAdvanceWarning: "يرجى الحجز قبل يومين (٤٨ ساعة) على الأقل.",
    },
    validation: {
      nameRequired: "الاسم مطلوب",
      nameMin: "يجب أن يكون الاسم على الأقل حرفين",
      emailRequired: "البريد الإلكتروني مطلوب",
      emailInvalid: "يرجى إدخال بريد إلكتروني صالح",
      phoneInvalid: "يرجى إدخال رقم هاتف صالح",
      messageRequired: "الرسالة مطلوبة",
      messageMin: "يجب أن تكون الرسالة على الأقل ١٠ أحرف",
    },
    calendar: {
      today: "اليوم",
      tomorrow: "غدًا",
      selectDate: "اختر تاريخًا",
      noAvailableDates: "لا توجد تواريخ متاحة في الأشهر الستة القادمة",
    },
    timezone: {
      label: "منطقتك الزمنية",
      autoDetected: "تم الكشف تلقائيًا",
    },
  },
} as const;

/**
 * Validation rules
 */
export const VALIDATION_RULES = {
  name: {
    minLength: 2,
    maxLength: 100,
    pattern: /^[a-zA-Z\u0600-\u06FF\s'-]+$/,
  },
  email: {
    // RFC 5322 compliant email regex (simplified)
    pattern:
      /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
  },
  phone: {
    // International phone format (optional)
    pattern: /^[\d\s\-+()]*$/,
    minLength: 10,
    maxLength: 20,
  },
  message: {
    minLength: 10,
    maxLength: 1000,
  },
} as const;

/**
 * Feature flags
 */
export const FEATURES = {
  /** Enable Outlook calendar integration */
  outlookIntegration: false,

  /** Enable Google Calendar integration */
  googleCalendar: false,

  /** Enable SMS notifications */
  smsNotifications: false,

  /** Enable automatic timezone detection */
  autoTimezone: true,

  /** Enable booking draft persistence */
  draftPersistence: true,

  /** Enable analytics tracking */
  analytics: true,
} as const;

/**
 * Analytics events
 */
export const ANALYTICS_EVENTS = {
  SCHEDULER_OPENED: "scheduler_opened",
  DURATION_SELECTED: "duration_selected",
  DATE_SELECTED: "date_selected",
  TIME_SELECTED: "time_selected",
  FORM_STARTED: "form_started",
  FORM_COMPLETED: "form_completed",
  BOOKING_CONFIRMED: "booking_confirmed",
  BOOKING_FAILED: "booking_failed",
  BOOKING_CANCELLED: "booking_cancelled",
} as const;

/**
 * Export default config for convenience
 */
export default SCHEDULER_CONFIG;
