"use client";

import { useState } from "react";
import type { Lang } from "@/lib/config";

interface MessageSchedulerProps {
  lang: Lang;
}

export default function MessageScheduler({ lang }: MessageSchedulerProps) {
  const [scheduleType, setScheduleType] = useState<
    "immediate" | "scheduled" | "recurring"
  >("immediate");
  const [selectedDate, setSelectedDate] = useState("");
  const [selectedTime, setSelectedTime] = useState("");
  const isRTL = lang === "ar";

  const content = {
    ar: {
      title: "جدولة الرسائل",
      subtitle: "أرسل رسائلك في الوقت المثالي",
      immediate: "إرسال فوري",
      scheduled: "جدولة موعد",
      recurring: "إرسال متكرر",
      selectDate: "اختر التاريخ",
      selectTime: "اختر الوقت",
      timezone: "المنطقة الزمنية",
      frequency: "التكرار",
      frequencies: {
        daily: "يومي",
        weekly: "أسبوعي",
        monthly: "شهري",
        custom: "مخصص",
      },
      respectHours: "احترام أوقات الراحة",
      respectPrayer: "احترام أوقات الصلاة",
      optimizeTime: "تحسين وقت الإرسال تلقائياً",
      scheduleBtn: "جدولة الإرسال",
      sendNowBtn: "إرسال الآن",
      upcoming: {
        title: "الرسائل المجدولة",
        items: [
          {
            id: 1,
            template: "تذكير موعد - 24 ساعة",
            recipients: 245,
            date: "2025-09-27",
            time: "09:00",
            status: "pending",
          },
          {
            id: 2,
            template: "عرض خاص شهري",
            recipients: 1523,
            date: "2025-09-28",
            time: "10:30",
            status: "pending",
          },
          {
            id: 3,
            template: "متابعة ما بعد الزيارة",
            recipients: 89,
            date: "2025-09-29",
            time: "14:00",
            status: "pending",
          },
        ],
      },
      recipients: "مستلم",
      status: {
        pending: "قيد الانتظار",
        sent: "تم الإرسال",
        failed: "فشل",
      },
      actions: "إجراءات",
      edit: "تعديل",
      cancel: "إلغاء",
    },
    en: {
      title: "Message Scheduler",
      subtitle: "Send your messages at the perfect time",
      immediate: "Send Immediately",
      scheduled: "Schedule for Later",
      recurring: "Recurring Send",
      selectDate: "Select Date",
      selectTime: "Select Time",
      timezone: "Timezone",
      frequency: "Frequency",
      frequencies: {
        daily: "Daily",
        weekly: "Weekly",
        monthly: "Monthly",
        custom: "Custom",
      },
      respectHours: "Respect Quiet Hours",
      respectPrayer: "Respect Prayer Times",
      optimizeTime: "Auto-optimize Send Time",
      scheduleBtn: "Schedule Send",
      sendNowBtn: "Send Now",
      upcoming: {
        title: "Upcoming Scheduled Messages",
        items: [
          {
            id: 1,
            template: "Appointment Reminder - 24h",
            recipients: 245,
            date: "2025-09-27",
            time: "09:00",
            status: "pending",
          },
          {
            id: 2,
            template: "Monthly Special Offer",
            recipients: 1523,
            date: "2025-09-28",
            time: "10:30",
            status: "pending",
          },
          {
            id: 3,
            template: "Post-Visit Follow-up",
            recipients: 89,
            date: "2025-09-29",
            time: "14:00",
            status: "pending",
          },
        ],
      },
      recipients: "recipients",
      status: {
        pending: "Pending",
        sent: "Sent",
        failed: "Failed",
      },
      actions: "Actions",
      edit: "Edit",
      cancel: "Cancel",
    },
  };

  const t = content[lang];

  const getStatusColor = (status: string) => {
    switch (status) {
      case "pending":
        return "bg-yellow-100 text-yellow-700 dark:bg-yellow-900/30 dark:text-yellow-400";
      case "sent":
        return "bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400";
      case "failed":
        return "bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400";
      default:
        return "bg-gray-100 text-gray-700";
    }
  };

  return (
    <div className={`space-y-8 ${isRTL ? "text-right" : "text-left"}`}>
      {/* Header */}
      <div className="text-center space-y-3">
        <h3 className="text-3xl font-black text-brand-ink dark:text-white">
          {t.title}
        </h3>
        <p className="text-lg text-gray-600 dark:text-gray-300">{t.subtitle}</p>
      </div>

      {/* Schedule Type Selector */}
      <div className="flex flex-wrap justify-center gap-4">
        <button
          onClick={() => setScheduleType("immediate")}
          className={`px-6 py-4 rounded-xl font-semibold transition-all ${
            scheduleType === "immediate"
              ? "bg-brand-green text-white shadow-lg scale-105"
              : "bg-white dark:bg-slate-800 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-slate-700"
          }`}
        >
          <div className="text-2xl mb-2">⚡</div>
          {t.immediate}
        </button>
        <button
          onClick={() => setScheduleType("scheduled")}
          className={`px-6 py-4 rounded-xl font-semibold transition-all ${
            scheduleType === "scheduled"
              ? "bg-brand-green text-white shadow-lg scale-105"
              : "bg-white dark:bg-slate-800 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-slate-700"
          }`}
        >
          <div className="text-2xl mb-2">📅</div>
          {t.scheduled}
        </button>
        <button
          onClick={() => setScheduleType("recurring")}
          className={`px-6 py-4 rounded-xl font-semibold transition-all ${
            scheduleType === "recurring"
              ? "bg-brand-green text-white shadow-lg scale-105"
              : "bg-white dark:bg-slate-800 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-slate-700"
          }`}
        >
          <div className="text-2xl mb-2">🔄</div>
          {t.recurring}
        </button>
      </div>

      {/* Schedule Options */}
      <div className="rounded-2xl bg-white dark:bg-slate-800 p-8 shadow-lg">
        {scheduleType === "immediate" && (
          <div className="text-center space-y-4">
            <div className="text-6xl mb-4">⚡</div>
            <p className="text-lg text-gray-600 dark:text-gray-300">
              {lang === "ar"
                ? "سيتم إرسال رسالتك فوراً بعد الضغط على زر الإرسال"
                : "Your message will be sent immediately after clicking the send button"}
            </p>
            <button className="bg-brand-green hover:bg-brand-greenHover text-white font-bold py-4 px-8 rounded-xl transition-all shadow-lg hover:shadow-xl">
              {t.sendNowBtn}
            </button>
          </div>
        )}

        {scheduleType === "scheduled" && (
          <div className="space-y-6">
            <div className="grid md:grid-cols-2 gap-6">
              <div>
                <label className="block text-sm font-semibold text-gray-700 dark:text-gray-300 mb-2">
                  {t.selectDate}
                </label>
                <input
                  type="date"
                  value={selectedDate}
                  onChange={(e) => setSelectedDate(e.target.value)}
                  className="w-full px-4 py-3 rounded-xl border-2 border-gray-200 dark:border-gray-700 bg-white dark:bg-slate-700 text-gray-900 dark:text-white focus:border-brand-green focus:ring-2 focus:ring-brand-green/20 transition-all"
                />
              </div>
              <div>
                <label className="block text-sm font-semibold text-gray-700 dark:text-gray-300 mb-2">
                  {t.selectTime}
                </label>
                <input
                  type="time"
                  value={selectedTime}
                  onChange={(e) => setSelectedTime(e.target.value)}
                  className="w-full px-4 py-3 rounded-xl border-2 border-gray-200 dark:border-gray-700 bg-white dark:bg-slate-700 text-gray-900 dark:text-white focus:border-brand-green focus:ring-2 focus:ring-brand-green/20 transition-all"
                />
              </div>
            </div>

            <div>
              <label className="block text-sm font-semibold text-gray-700 dark:text-gray-300 mb-2">
                {t.timezone}
              </label>
              <select className="w-full px-4 py-3 rounded-xl border-2 border-gray-200 dark:border-gray-700 bg-white dark:bg-slate-700 text-gray-900 dark:text-white focus:border-brand-green focus:ring-2 focus:ring-brand-green/20 transition-all">
                <option>GMT+3 (Riyadh, Kuwait, Doha)</option>
                <option>GMT+4 (Abu Dhabi, Dubai, Muscat)</option>
              </select>
            </div>

            <div className="space-y-3 pt-4 border-t border-gray-200 dark:border-gray-700">
              <label className="flex items-center gap-3 cursor-pointer">
                <input
                  type="checkbox"
                  className="w-5 h-5 rounded text-brand-green focus:ring-brand-green"
                  defaultChecked
                />
                <span className="text-sm font-medium text-gray-700 dark:text-gray-300">
                  {t.respectHours}
                </span>
              </label>
              <label className="flex items-center gap-3 cursor-pointer">
                <input
                  type="checkbox"
                  className="w-5 h-5 rounded text-brand-green focus:ring-brand-green"
                  defaultChecked
                />
                <span className="text-sm font-medium text-gray-700 dark:text-gray-300">
                  {t.respectPrayer}
                </span>
              </label>
              <label className="flex items-center gap-3 cursor-pointer">
                <input
                  type="checkbox"
                  className="w-5 h-5 rounded text-brand-green focus:ring-brand-green"
                />
                <span className="text-sm font-medium text-gray-700 dark:text-gray-300">
                  {t.optimizeTime}
                </span>
              </label>
            </div>

            <button className="w-full bg-brand-green hover:bg-brand-greenHover text-white font-bold py-4 px-8 rounded-xl transition-all shadow-lg hover:shadow-xl">
              {t.scheduleBtn}
            </button>
          </div>
        )}

        {scheduleType === "recurring" && (
          <div className="space-y-6">
            <div>
              <label className="block text-sm font-semibold text-gray-700 dark:text-gray-300 mb-2">
                {t.frequency}
              </label>
              <select className="w-full px-4 py-3 rounded-xl border-2 border-gray-200 dark:border-gray-700 bg-white dark:bg-slate-700 text-gray-900 dark:text-white focus:border-brand-green focus:ring-2 focus:ring-brand-green/20 transition-all">
                {Object.values(t.frequencies).map((freq, idx) => (
                  <option key={idx}>{freq}</option>
                ))}
              </select>
            </div>

            <div className="grid md:grid-cols-2 gap-6">
              <div>
                <label className="block text-sm font-semibold text-gray-700 dark:text-gray-300 mb-2">
                  {lang === "ar" ? "تاريخ البدء" : "Start Date"}
                </label>
                <input
                  type="date"
                  className="w-full px-4 py-3 rounded-xl border-2 border-gray-200 dark:border-gray-700 bg-white dark:bg-slate-700 text-gray-900 dark:text-white focus:border-brand-green focus:ring-2 focus:ring-brand-green/20 transition-all"
                />
              </div>
              <div>
                <label className="block text-sm font-semibold text-gray-700 dark:text-gray-300 mb-2">
                  {t.selectTime}
                </label>
                <input
                  type="time"
                  className="w-full px-4 py-3 rounded-xl border-2 border-gray-200 dark:border-gray-700 bg-white dark:bg-slate-700 text-gray-900 dark:text-white focus:border-brand-green focus:ring-2 focus:ring-brand-green/20 transition-all"
                />
              </div>
            </div>

            <button className="w-full bg-brand-green hover:bg-brand-greenHover text-white font-bold py-4 px-8 rounded-xl transition-all shadow-lg hover:shadow-xl">
              {lang === "ar"
                ? "إنشاء حملة متكررة"
                : "Create Recurring Campaign"}
            </button>
          </div>
        )}
      </div>

      {/* Upcoming Scheduled Messages */}
      <div className="rounded-2xl bg-white dark:bg-slate-800 p-8 shadow-lg">
        <h4 className="text-xl font-bold text-brand-ink dark:text-white mb-6">
          {t.upcoming.title}
        </h4>
        <div className="space-y-4">
          {t.upcoming.items.map((item) => (
            <div
              key={item.id}
              className="flex flex-wrap items-center justify-between gap-4 p-4 rounded-xl bg-gray-50 dark:bg-slate-700 hover:bg-gray-100 dark:hover:bg-slate-600 transition-all"
            >
              <div className="flex-1 min-w-[200px]">
                <div className="font-semibold text-gray-900 dark:text-white">
                  {item.template}
                </div>
                <div className="text-sm text-gray-600 dark:text-gray-400 mt-1">
                  {item.recipients} {t.recipients}
                </div>
              </div>
              <div className="flex items-center gap-4">
                <div className="text-right">
                  <div className="text-sm font-medium text-gray-900 dark:text-white">
                    {item.date}
                  </div>
                  <div className="text-sm text-gray-600 dark:text-gray-400">
                    {item.time}
                  </div>
                </div>
                <span
                  className={`px-3 py-1 rounded-full text-xs font-bold ${getStatusColor(item.status)}`}
                >
                  {t.status[item.status as keyof typeof t.status]}
                </span>
                <div className="flex gap-2">
                  <button className="text-sm font-semibold text-brand-green hover:text-brand-greenHover transition-colors">
                    {t.edit}
                  </button>
                  <span className="text-gray-300 dark:text-gray-600">|</span>
                  <button className="text-sm font-semibold text-red-600 hover:text-red-700 transition-colors">
                    {t.cancel}
                  </button>
                </div>
              </div>
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}
