"use client";

import { useState } from "react";
import {
  Dialog,
  DialogContent,
  DialogTitle,
  DialogDescription,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import {
  Repeat,
  Calendar,
  Sparkles,
  CheckCheck,
  CalendarDays,
  Loader2,
} from "lucide-react";
import { formatDate } from "@/lib/utils";

interface RecurringScheduleDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  meetingTitle?: string;
  scheduledAt?: string;
  onConfirm: (applyToSeries: boolean) => Promise<void> | void;
  isSubmitting?: boolean;
}

export function RecurringScheduleDialog({
  open,
  onOpenChange,
  meetingTitle,
  scheduledAt,
  onConfirm,
  isSubmitting = false,
}: RecurringScheduleDialogProps) {
  const [selectedAction, setSelectedAction] = useState<"series" | "single" | null>(null);

  const handleSelect = async (applyToSeries: boolean) => {
    setSelectedAction(applyToSeries ? "series" : "single");
    try {
      await onConfirm(applyToSeries);
    } finally {
      setSelectedAction(null);
    }
  };

  const handleCancel = () => {
    if (isSubmitting) return;
    onOpenChange(false);
  };

  return (
    <Dialog open={open} onOpenChange={handleCancel}>
      <DialogContent className="sm:max-w-md p-0 overflow-hidden border border-gray-100 dark:border-gray-800 shadow-2xl rounded-2xl">
        {/* Top Gradient Header Banner */}
        <div className="relative bg-gradient-to-br from-blue-600 via-indigo-600 to-purple-600 px-6 pt-6 pb-5 text-white">
          <div className="flex items-center gap-3">
            <div className="flex h-11 w-11 shrink-0 items-center justify-center rounded-2xl bg-white/20 backdrop-blur-md border border-white/25 shadow-inner">
              <Repeat className="h-5 w-5 text-white animate-spin-slow" />
            </div>
            <div>
              <div className="inline-flex items-center gap-1.5 px-2 py-0.5 rounded-full bg-white/15 text-[11px] font-medium text-white/90 backdrop-blur-sm mb-1">
                <Sparkles className="h-3 w-3 text-amber-300" />
                Recurring Event Preference
              </div>
              <DialogTitle className="text-lg font-bold text-white tracking-tight leading-snug">
                Auto-Schedule Meeting Bot
              </DialogTitle>
            </div>
          </div>
          <DialogDescription className="text-white/80 text-xs mt-2 leading-relaxed">
            Configure how note-taking bots are deployed for this recurring meeting schedule.
          </DialogDescription>
        </div>

        {/* Meeting Information Details */}
        <div className="p-6 space-y-5 bg-white dark:bg-gray-900">
          <div className="rounded-xl border border-gray-100 dark:border-gray-800 bg-gray-50/70 dark:bg-gray-950/40 p-3.5 space-y-1.5">
            <div className="flex items-center justify-between gap-2">
              <span className="text-xs font-semibold text-gray-500 dark:text-gray-400">
                Event
              </span>
              <Badge variant="default" className="text-[10px] px-2 py-0.5 gap-1 font-medium">
                <Repeat className="h-2.5 w-2.5" />
                Recurring Series
              </Badge>
            </div>
            <p className="text-sm font-semibold text-gray-900 dark:text-gray-100 truncate">
              {meetingTitle || "Scheduled Recurring Meeting"}
            </p>
            {scheduledAt && (
              <p className="text-xs text-gray-500 dark:text-gray-400 flex items-center gap-1.5 pt-0.5">
                <Calendar className="h-3.5 w-3.5 text-blue-500" />
                {formatDate(scheduledAt)}
              </p>
            )}
          </div>

          {/* Primary Question */}
          <div className="text-center sm:text-left">
            <h4 className="text-sm font-bold text-gray-900 dark:text-gray-100 leading-snug">
              Should I create all the bots automatically for this recurring event every time?
            </h4>
            <p className="text-xs text-gray-500 dark:text-gray-400 mt-1">
              Select whether you want bots generated for every upcoming occurrence or just this one.
            </p>
          </div>

          {/* Action Choice Options */}
          <div className="space-y-3">
            {/* Option 1: YES - All Future Meetings */}
            <button
              type="button"
              disabled={isSubmitting}
              onClick={() => handleSelect(true)}
              className="w-full group text-left relative flex items-start gap-3 p-3.5 rounded-xl border-2 border-blue-600/30 dark:border-blue-500/30 hover:border-blue-600 dark:hover:border-blue-500 bg-blue-50/40 dark:bg-blue-950/20 hover:bg-blue-50 dark:hover:bg-blue-950/40 transition-all duration-200 cursor-pointer disabled:opacity-60 disabled:cursor-not-allowed shadow-sm hover:shadow"
            >
              <div className="flex h-9 w-9 shrink-0 items-center justify-center rounded-xl bg-blue-600 text-white shadow-sm mt-0.5 group-hover:scale-105 transition-transform">
                {isSubmitting && selectedAction === "series" ? (
                  <Loader2 className="h-4 w-4 animate-spin text-white" />
                ) : (
                  <CheckCheck className="h-4 w-4" />
                )}
              </div>
              <div className="flex-1 min-w-0">
                <div className="flex items-center gap-2">
                  <span className="text-xs font-bold text-blue-900 dark:text-blue-100">
                    Yes, All Future Meetings
                  </span>
                  <span className="text-[10px] font-semibold text-blue-600 dark:text-blue-400 bg-blue-100 dark:bg-blue-900/60 px-1.5 py-0.2 rounded-full">
                    Recommended
                  </span>
                </div>
                <p className="text-[11px] text-blue-700/80 dark:text-blue-300/80 mt-0.5 leading-relaxed">
                  Automatically create and join AI note-taker bots for all future occurrences of this series.
                </p>
              </div>
            </button>

            {/* Option 2: NO - Only This Meeting */}
            <button
              type="button"
              disabled={isSubmitting}
              onClick={() => handleSelect(false)}
              className="w-full group text-left relative flex items-start gap-3 p-3.5 rounded-xl border border-gray-200 dark:border-gray-800 hover:border-gray-300 dark:hover:border-gray-700 bg-gray-50/50 dark:bg-gray-900/50 hover:bg-gray-100/70 dark:hover:bg-gray-800/50 transition-all duration-200 cursor-pointer disabled:opacity-60 disabled:cursor-not-allowed shadow-sm"
            >
              <div className="flex h-9 w-9 shrink-0 items-center justify-center rounded-xl bg-gray-200 dark:bg-gray-800 text-gray-700 dark:text-gray-300 shadow-sm mt-0.5 group-hover:scale-105 transition-transform">
                {isSubmitting && selectedAction === "single" ? (
                  <Loader2 className="h-4 w-4 animate-spin text-gray-600 dark:text-gray-300" />
                ) : (
                  <CalendarDays className="h-4 w-4" />
                )}
              </div>
              <div className="flex-1 min-w-0">
                <div className="flex items-center gap-2">
                  <span className="text-xs font-bold text-gray-900 dark:text-gray-100">
                    No, Only This Meeting
                  </span>
                </div>
                <p className="text-[11px] text-gray-500 dark:text-gray-400 mt-0.5 leading-relaxed">
                  Only schedule the bot for this single meeting. You will be prompted again for future sessions.
                </p>
              </div>
            </button>
          </div>

          {/* Footer Dismiss Button */}
          <div className="pt-2 flex justify-end border-t border-gray-100 dark:border-gray-800">
            <Button
              type="button"
              variant="ghost"
              size="sm"
              disabled={isSubmitting}
              onClick={handleCancel}
              className="text-xs text-gray-500 hover:text-gray-800 dark:text-gray-400 dark:hover:text-gray-200"
            >
              Cancel (Leave Bot Off)
            </Button>
          </div>
        </div>
      </DialogContent>
    </Dialog>
  );
}
