"use client";

import { useState } from "react";
import { X, Bell, BellOff } from "lucide-react";

interface SaveSearchModalProps {
    isOpen: boolean;
    onClose: () => void;
    onSave: (name: string, alertFrequency: "none" | "daily" | "weekly") => void;
    defaultName?: string;
    isSaving?: boolean;
}

export function SaveSearchModal({
    isOpen,
    onClose,
    onSave,
    defaultName = "",
    isSaving = false,
}: SaveSearchModalProps) {
    const [name, setName] = useState(defaultName);
    const [alertFrequency, setAlertFrequency] = useState<"none" | "daily" | "weekly">("none");

    if (!isOpen) return null;

    const handleSave = () => {
        if (!name.trim()) return;
        onSave(name.trim(), alertFrequency);
    };

    return (
        <div className="fixed inset-0 z-50 flex items-center justify-center">
            <div className="absolute inset-0 bg-black/40 backdrop-blur-sm" onClick={onClose} />
            <div className="relative w-full max-w-md rounded-2xl bg-white p-6 shadow-2xl animate-in fade-in zoom-in-95 duration-200 mx-4">
                {/* Header */}
                <div className="flex items-center justify-between mb-5">
                    <h3 className="text-lg font-bold text-slate-900">Save This Search</h3>
                    <button
                        onClick={onClose}
                        className="p-1.5 rounded-full hover:bg-slate-100 transition-colors"
                        aria-label="Close"
                    >
                        <X className="h-5 w-5 text-slate-400" />
                    </button>
                </div>

                {/* Name input */}
                <div className="mb-5">
                    <label className="block text-sm font-semibold text-slate-700 mb-1.5">
                        Search Name
                    </label>
                    <input
                        type="text"
                        value={name}
                        onChange={(e) => setName(e.target.value)}
                        placeholder="e.g. Remote Marketing Jobs in Lagos"
                        className="w-full rounded-xl border border-slate-200 px-4 py-2.5 text-sm outline-none focus:border-blue-400 focus:ring-2 focus:ring-blue-100 transition-all"
                        autoFocus
                    />
                </div>

                {/* Alert frequency */}
                <div className="mb-6">
                    <label className="block text-sm font-semibold text-slate-700 mb-2">
                        Get alerts when new jobs match
                    </label>
                    <div className="space-y-2">
                        {[
                            { value: "none" as const, label: "No alerts", icon: <BellOff className="h-4 w-4" />, desc: "I'll check manually" },
                            { value: "daily" as const, label: "Daily email", icon: <Bell className="h-4 w-4" />, desc: "Get notified every morning" },
                            { value: "weekly" as const, label: "Weekly email", icon: <Bell className="h-4 w-4" />, desc: "Weekly summary on Mondays" },
                        ].map((option) => (
                            <label
                                key={option.value}
                                className={`flex items-center gap-3 rounded-xl border p-3 cursor-pointer transition-all ${alertFrequency === option.value
                                        ? "border-blue-300 bg-blue-50/50 ring-1 ring-blue-200"
                                        : "border-slate-200 hover:border-slate-300 hover:bg-slate-50"
                                    }`}
                            >
                                <input
                                    type="radio"
                                    name="alertFrequency"
                                    value={option.value}
                                    checked={alertFrequency === option.value}
                                    onChange={() => setAlertFrequency(option.value)}
                                    className="sr-only"
                                />
                                <div className={`p-1.5 rounded-lg ${alertFrequency === option.value ? "bg-blue-100 text-blue-600" : "bg-slate-100 text-slate-400"}`}>
                                    {option.icon}
                                </div>
                                <div className="flex-1">
                                    <p className="text-sm font-semibold text-slate-800">{option.label}</p>
                                    <p className="text-xs text-slate-500">{option.desc}</p>
                                </div>
                                <div className={`h-4 w-4 rounded-full border-2 flex items-center justify-center ${alertFrequency === option.value ? "border-blue-500" : "border-slate-300"
                                    }`}>
                                    {alertFrequency === option.value && (
                                        <div className="h-2 w-2 rounded-full bg-blue-500" />
                                    )}
                                </div>
                            </label>
                        ))}
                    </div>
                </div>

                {/* Actions */}
                <div className="flex items-center gap-3">
                    <button
                        onClick={onClose}
                        className="flex-1 rounded-xl border border-slate-200 px-4 py-2.5 text-sm font-semibold text-slate-600 hover:bg-slate-50 transition-colors"
                    >
                        Cancel
                    </button>
                    <button
                        onClick={handleSave}
                        disabled={!name.trim() || isSaving}
                        className="flex-1 rounded-xl bg-blue-600 px-4 py-2.5 text-sm font-bold text-white hover:bg-blue-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed shadow-lg shadow-blue-500/20"
                    >
                        {isSaving ? "Saving..." : "Save Search"}
                    </button>
                </div>
            </div>
        </div>
    );
}
