'use client';

import { useState, useEffect } from 'react';
import {
    X, CheckCircle, ShieldCheck, AlertTriangle,
    Zap, Mail, Newspaper, Star, Search,
    ArrowRight, Loader2, Info
} from 'lucide-react';
import { motion, AnimatePresence } from 'framer-motion';
import { useBulkSelection } from '../../context/BulkSelectionContext';
import { cn } from '@/lib/utils';

interface BulkApproveModalProps {
    isOpen: boolean;
    onClose: () => void;
    onApprove: (data: any) => Promise<void>;
    isProcessing: boolean;
}

export function BulkApproveModal({ isOpen, onClose, onApprove, isProcessing }: BulkApproveModalProps) {
    const { selectedCount } = useBulkSelection();
    const [scanProgress, setScanProgress] = useState(0);
    const [isScanning, setIsScanning] = useState(true);

    // Approval Options
    const [featureAll, setFeatureAll] = useState(false);
    const [notifyPremium, setNotifyPremium] = useState(true);
    const [addToNewsletter, setAddToNewsletter] = useState(false);

    useEffect(() => {
        if (isOpen && isScanning) {
            const interval = setInterval(() => {
                setScanProgress(prev => {
                    if (prev >= 100) {
                        clearInterval(interval);
                        setTimeout(() => setIsScanning(false), 500);
                        return 100;
                    }
                    return prev + Math.random() * 15;
                });
            }, 300);
            return () => clearInterval(interval);
        }
    }, [isOpen, isScanning]);

    if (!isOpen) return null;

    return (
        <div className="fixed inset-0 z-[60] flex items-center justify-center p-4 bg-slate-900/80 backdrop-blur-sm">
            <motion.div
                initial={{ scale: 0.9, opacity: 0 }}
                animate={{ scale: 1, opacity: 1 }}
                className="bg-white rounded-3xl shadow-2xl w-full max-w-2xl overflow-hidden flex flex-col"
            >
                {/* Header */}
                <div className="p-6 border-b border-slate-100 flex items-center justify-between bg-emerald-50/50">
                    <div className="flex items-center gap-3">
                        <div className="w-10 h-10 bg-emerald-100 text-emerald-600 rounded-xl flex items-center justify-center">
                            <ShieldCheck size={24} />
                        </div>
                        <div>
                            <h2 className="text-xl font-extrabold text-slate-900 tracking-tight">Bulk Approval Gateway</h2>
                            <p className="text-sm text-emerald-700 font-bold uppercase tracking-wider text-[10px]">
                                Security Sweep for {selectedCount} Listings
                            </p>
                        </div>
                    </div>
                    <button onClick={onClose} className="p-2 hover:bg-slate-200 rounded-full transition-colors">
                        <X size={20} />
                    </button>
                </div>

                <div className="p-8">
                    <AnimatePresence mode="wait">
                        {isScanning ? (
                            <motion.div
                                key="scanning"
                                initial={{ opacity: 0 }}
                                animate={{ opacity: 1 }}
                                exit={{ opacity: 0 }}
                                className="py-12 flex flex-col items-center text-center space-y-6"
                            >
                                <div className="relative w-24 h-24">
                                    <div className="absolute inset-0 border-4 border-emerald-100 rounded-full"></div>
                                    <svg className="w-full h-full -rotate-90">
                                        <circle
                                            cx="48"
                                            cy="48"
                                            r="44"
                                            fill="none"
                                            stroke="currentColor"
                                            strokeWidth="8"
                                            strokeDasharray={276}
                                            strokeDashoffset={276 - (276 * scanProgress) / 100}
                                            className="text-emerald-500 transition-all duration-300 ease-out"
                                        />
                                    </svg>
                                    <div className="absolute inset-0 flex items-center justify-center">
                                        <Search className="text-emerald-500 animate-pulse" size={32} />
                                    </div>
                                </div>
                                <div className="space-y-2">
                                    <h3 className="text-lg font-bold text-slate-900">Pre-Approval Vulnerability Scan</h3>
                                    <p className="text-sm text-slate-500 max-w-sm">Checking headers, domain reputation, and content clusters for batch integrity...</p>
                                </div>
                                <div className="w-full bg-slate-100 rounded-full h-2 overflow-hidden max-w-xs">
                                    <motion.div
                                        className="bg-emerald-500 h-full"
                                        initial={{ width: 0 }}
                                        animate={{ width: `${scanProgress}%` }}
                                    />
                                </div>
                            </motion.div>
                        ) : (
                            <motion.div
                                key="summary"
                                initial={{ opacity: 0, y: 20 }}
                                animate={{ opacity: 1, y: 0 }}
                                className="space-y-8"
                            >
                                {/* Scan Summary Grid */}
                                <div className="grid grid-cols-2 gap-4">
                                    <div className="p-4 rounded-2xl bg-emerald-50 border border-emerald-100">
                                        <div className="flex items-center gap-2 text-emerald-700 mb-1">
                                            <CheckCircle size={16} />
                                            <span className="text-xs font-black uppercase tracking-widest">Safe to Post</span>
                                        </div>
                                        <p className="text-2xl font-black text-emerald-900">{Math.floor(selectedCount * 0.9)} <span className="text-sm font-bold opacity-60">Jobs</span></p>
                                    </div>
                                    <div className="p-4 rounded-2xl bg-amber-50 border border-amber-100">
                                        <div className="flex items-center gap-2 text-amber-700 mb-1">
                                            <AlertTriangle size={16} />
                                            <span className="text-xs font-black uppercase tracking-widest">Low Risk Warnings</span>
                                        </div>
                                        <p className="text-2xl font-black text-amber-900">{Math.ceil(selectedCount * 0.1)} <span className="text-sm font-bold opacity-60">Jobs</span></p>
                                    </div>
                                </div>

                                {/* Options */}
                                <div className="space-y-4">
                                    <h3 className="text-sm font-black text-slate-400 uppercase tracking-widest mb-4">Post-Approval Dispatch Options</h3>

                                    <div className="space-y-3">
                                        <button
                                            onClick={() => setFeatureAll(!featureAll)}
                                            className={cn(
                                                "w-full flex items-center justify-between p-4 rounded-2xl border-2 transition-all",
                                                featureAll ? "border-amber-400 bg-amber-50" : "border-slate-100 hover:border-slate-200"
                                            )}
                                        >
                                            <div className="flex items-center gap-4">
                                                <div className={cn("p-2 rounded-xl bg-white", featureAll ? "text-amber-500" : "text-slate-400")}>
                                                    <Star size={20} fill={featureAll ? "currentColor" : "none"} />
                                                </div>
                                                <div className="text-left">
                                                    <p className="font-bold text-slate-900 text-sm">Feature all approved jobs</p>
                                                    <p className="text-[10px] text-slate-500 font-medium">Pin to top of search results for 48 hours</p>
                                                </div>
                                            </div>
                                            <div className={cn("w-6 h-6 rounded-full border-2 flex items-center justify-center transition-all", featureAll ? "bg-amber-400 border-amber-400" : "border-slate-200")}>
                                                {featureAll && <CheckCircle size={14} className="text-white" />}
                                            </div>
                                        </button>

                                        <button
                                            onClick={() => setNotifyPremium(!notifyPremium)}
                                            className={cn(
                                                "w-full flex items-center justify-between p-4 rounded-2xl border-2 transition-all",
                                                notifyPremium ? "border-blue-400 bg-blue-50" : "border-slate-100 hover:border-slate-200"
                                            )}
                                        >
                                            <div className="flex items-center gap-4">
                                                <div className={cn("p-2 rounded-xl bg-white", notifyPremium ? "text-blue-500" : "text-slate-400")}>
                                                    <Zap size={20} />
                                                </div>
                                                <div className="text-left">
                                                    <p className="font-bold text-slate-900 text-sm">Notify Premium Candidates</p>
                                                    <p className="text-[10px] text-slate-500 font-medium">Instant alerts to matched high-value subscribers</p>
                                                </div>
                                            </div>
                                            <div className={cn("w-6 h-6 rounded-full border-2 flex items-center justify-center transition-all", notifyPremium ? "bg-blue-400 border-blue-400" : "border-slate-200")}>
                                                {notifyPremium && <CheckCircle size={14} className="text-white" />}
                                            </div>
                                        </button>

                                        <button
                                            onClick={() => setAddToNewsletter(!addToNewsletter)}
                                            className={cn(
                                                "w-full flex items-center justify-between p-4 rounded-2xl border-2 transition-all",
                                                addToNewsletter ? "border-indigo-400 bg-indigo-50" : "border-slate-100 hover:border-slate-200"
                                            )}
                                        >
                                            <div className="flex items-center gap-4">
                                                <div className={cn("p-2 rounded-xl bg-white", addToNewsletter ? "text-indigo-500" : "text-slate-400")}>
                                                    <Newspaper size={20} />
                                                </div>
                                                <div className="text-left">
                                                    <p className="font-bold text-slate-900 text-sm">Include in Weekly Digest</p>
                                                    <p className="text-[10px] text-slate-500 font-medium">Add all approved listings to next Monday newsletter</p>
                                                </div>
                                            </div>
                                            <div className={cn("w-6 h-6 rounded-full border-2 flex items-center justify-center transition-all", addToNewsletter ? "bg-indigo-400 border-indigo-400" : "border-slate-200")}>
                                                {addToNewsletter && <CheckCircle size={14} className="text-white" />}
                                            </div>
                                        </button>
                                    </div>
                                </div>
                            </motion.div>
                        )}
                    </AnimatePresence>
                </div>

                {/* Footer */}
                <div className="p-6 border-t border-slate-100 flex items-center justify-between bg-slate-50">
                    <button
                        onClick={onClose}
                        className="px-6 py-3 text-slate-500 font-bold hover:bg-slate-100 rounded-2xl transition-all"
                    >
                        Abort
                    </button>

                    <button
                        onClick={() => onApprove({ featureAll, notifyPremium, addToNewsletter })}
                        disabled={isScanning || isProcessing}
                        className="flex-1 max-w-[240px] flex items-center justify-center gap-2 px-6 py-4 bg-emerald-600 disabled:bg-slate-200 text-white rounded-2xl font-black uppercase text-lg hover:bg-emerald-700 transition-all shadow-xl shadow-emerald-100 active:scale-95 disabled:shadow-none"
                    >
                        {isProcessing ? (
                            <Loader2 className="animate-spin" />
                        ) : (
                            <>
                                Release Batch
                                <ArrowRight size={24} />
                            </>
                        )}
                    </button>
                </div>
            </motion.div>
        </div>
    );
}
