"use client";

import { useState } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { ArrowRight, CheckCircle2, Globe2, User, Shield, Play, MonitorPlay, Users, Briefcase } from "lucide-react";
import { useCart } from "@/store/useCart";

type StudyType = "platform" | "individual" | "group" | "corporate";
type Level = "A1-A2" | "B1-B2" | "C1-C2";
type Language = "EN" | "RO" | "JP";
type Duration = "1m" | "3m" | "6m";

const PRICING = {
  platform: {
    "A1-A2": { RO: 700, EN: 800, JP: 1150 },
    "B1-B2": { RO: 800, EN: 1300, JP: 1600 },
    "C1-C2": { RO: 1000, EN: 1700, JP: 2000 },
  },
  individual: {
    "A1-A2": { "1m": { RO: 252, EN: 288, JP: 360 }, "3m": { RO: 756, EN: 864, JP: 1080 }, "6m": { RO: 1512, EN: 1728, JP: 2160 } },
    "B1-B2": { "1m": { RO: 270, EN: 324, JP: 540 }, "3m": { RO: 810, EN: 972, JP: 1620 }, "6m": { RO: 1620, EN: 1944, JP: 3240 } },
    "C1-C2": { "1m": { RO: 288, EN: 432, JP: 648 }, "3m": { RO: 864, EN: 1296, JP: 1944 }, "6m": { RO: 1728, EN: 2592, JP: 4320 } }
  },
  group: {
    "A1-A2": { "1m": { RO: 200, EN: 230, JP: 280 }, "3m": { RO: 600, EN: 690, JP: 840 }, "6m": { RO: 1200, EN: 1380, JP: 1680 } },
    "B1-B2": { "1m": { RO: 210, EN: 250, JP: 430 }, "3m": { RO: 630, EN: 750, JP: 1290 }, "6m": { RO: 1260, EN: 1500, JP: 2580 } },
    "C1-C2": { "1m": { RO: 230, EN: 340, JP: 510 }, "3m": { RO: 690, EN: 1020, JP: 1530 }, "6m": { RO: 1380, EN: 2040, JP: 3060 } }
  },
  corporate: {
    "A1-A2": { "1m": { RO: 220, EN: 250, JP: 320 }, "3m": { RO: 670, EN: 760, JP: 950 }, "6m": { RO: 2000, EN: 2290, JP: 2860 } },
    "B1-B2": { "1m": { RO: 240, EN: 290, JP: 470 }, "3m": { RO: 710, EN: 860, JP: 1430 }, "6m": { RO: 2140, EN: 2570, JP: 4290 } },
    "C1-C2": { "1m": { RO: 250, EN: 380, JP: 570 }, "3m": { RO: 760, EN: 1140, JP: 1710 }, "6m": { RO: 2290, EN: 3430, JP: 5710 } }
  }
};

export default function PackageConfigurator() {
  const { addItem, setIsOpen } = useCart();
  
  const [language, setLanguage] = useState<Language>("EN");
  const [level, setLevel] = useState<Level>("B1-B2");
  const [studyType, setStudyType] = useState<StudyType>("platform");
  const [duration, setDuration] = useState<Duration>("1m");
  const [employeeCount, setEmployeeCount] = useState<number>(10);

  // Calculate Price
  let basePrice = 0;
  if (studyType === "platform") {
    basePrice = PRICING.platform[level][language];
  } else if (studyType === "individual") {
    basePrice = PRICING.individual[level][duration][language];
  } else if (studyType === "group") {
    basePrice = PRICING.group[level][duration][language];
  } else {
    basePrice = PRICING.corporate[level][duration][language];
  }

  const totalPrice = studyType === "corporate" ? basePrice * employeeCount : basePrice;

  const handleEnroll = () => {
    const langName = language === "EN" ? "English" : language === "RO" ? "Romanian" : "Japanese";
    const typeName = studyType === "platform" ? "Platform Access" : studyType === "individual" ? "1-on-1 Tutoring" : studyType === "group" ? "Group Classes" : "Corporate Training";
    const durName = studyType === "platform" ? "Lifetime" : duration === "1m" ? "1 Month" : duration === "3m" ? "3 Months" : "6 Months";
    
    addItem({
      id: `${studyType}-${language}-${level}-${duration}-${Date.now()}`,
      title: `${langName} - ${typeName} (${level}) - ${durName}${studyType === 'corporate' ? ` (${employeeCount} Employees)` : ''}`,
      price: totalPrice,
      thumbnail: language === "JP" ? "https://flagcdn.com/w320/jp.png" : language === "RO" ? "https://flagcdn.com/w320/ro.png" : "https://flagcdn.com/w320/gb.png",
    } as any);
    setIsOpen(true);
  };

  return (
    <main className="min-h-[100dvh] bg-[#030408] text-white relative pt-24 md:pt-32 pb-32 px-4 md:px-8 overflow-hidden font-sans">
      
      {/* Ambient OLED Glows */}
      <div className="fixed inset-0 pointer-events-none z-0 overflow-hidden">
        <div className="absolute top-[5%] left-[10%] w-[50%] h-[50%] rounded-full bg-[#102d56]/20 blur-[140px]" />
        <div className="absolute top-[40%] right-[5%] w-[40%] h-[40%] rounded-full bg-[#dc2626]/15 blur-[150px]" />
        <div className="absolute inset-0 opacity-[0.02]" style={{ backgroundImage: "linear-gradient(rgba(255,255,255,0.1) 1px,transparent 1px),linear-gradient(90deg,rgba(255,255,255,0.1) 1px,transparent 1px)", backgroundSize: "40px 40px" }} />
      </div>

      <div className="max-w-7xl mx-auto relative z-10">
        
        {/* Page Title */}
        <motion.div initial={{ opacity: 0, y: 20 }} animate={{ opacity: 1, y: 0 }} className="mb-12 md:mb-16">
          <h1 className="text-4xl md:text-6xl font-black tracking-tight mb-4">
            Configure your <span className="text-transparent bg-clip-text bg-gradient-to-r from-[#ff4b4b] via-[#dc2626] to-[#7f1d1d]">Learning Journey</span>
          </h1>
          <p className="text-slate-400 text-lg max-w-2xl">
            Select your language, level, and preferred study method to instantly see your personalized investment. Add to cart to begin.
          </p>
        </motion.div>

        <div className="grid grid-cols-1 lg:grid-cols-12 gap-10 lg:gap-16 items-start">
          
          {/* LEFT COLUMN: CONFIGURATOR */}
          <div className="lg:col-span-8 space-y-12">
            
            {/* 1. Choose Language */}
            <motion.div initial={{ opacity: 0, x: -20 }} animate={{ opacity: 1, x: 0 }} transition={{ delay: 0.1 }}>
              <div className="flex items-center gap-4 mb-6">
                <div className="w-10 h-10 rounded-xl bg-[#dc2626]/10 border border-[#dc2626]/30 flex items-center justify-center font-bold text-[#ff4b4b] shadow-[0_0_15px_rgba(220,38,38,0.2)]">1</div>
                <div>
                  <h2 className="text-2xl font-bold">Select Language</h2>
                </div>
              </div>
              <div className="grid grid-cols-2 md:grid-cols-3 gap-4">
                {[
                  { id: "EN", label: "English", sub: "English", flag: "https://flagcdn.com/w80/gb.png" },
                  { id: "RO", label: "Romanian", sub: "Română", flag: "https://flagcdn.com/w80/ro.png" },
                  { id: "JP", label: "Japanese", sub: "日本語", flag: "https://flagcdn.com/w80/jp.png" }
                ].map((lang) => (
                  <button
                    key={lang.id}
                    onClick={() => setLanguage(lang.id as Language)}
                    className={`relative p-5 rounded-2xl border transition-all duration-300 flex flex-col items-center justify-center gap-3 text-center group overflow-hidden ${
                      language === lang.id 
                        ? "bg-[#dc2626]/10 border-[#dc2626] shadow-[0_10px_30px_rgba(220,38,38,0.15)]" 
                        : "bg-white/5 border-white/5 hover:border-white/15 hover:bg-white/10"
                    }`}
                  >
                    {language === lang.id && (
                      <motion.div layoutId="lang-check" className="absolute top-3 right-3 w-5 h-5 bg-[#dc2626] rounded-full flex items-center justify-center shadow-lg">
                        <CheckCircle2 className="w-3 h-3 text-white" />
                      </motion.div>
                    )}
                    <img src={lang.flag} alt={lang.id} className="w-10 h-7 object-cover rounded shadow-sm opacity-90 group-hover:opacity-100 transition-opacity" />
                    <div>
                      <div className="font-bold text-white tracking-wide">{lang.label}</div>
                      <div className="text-xs text-slate-400 mt-0.5">{lang.sub}</div>
                    </div>
                  </button>
                ))}
              </div>
            </motion.div>

            {/* 2. Choose Level */}
            <motion.div initial={{ opacity: 0, x: -20 }} animate={{ opacity: 1, x: 0 }} transition={{ delay: 0.2 }}>
              <div className="flex items-center gap-4 mb-6">
                <div className="w-10 h-10 rounded-xl bg-[#dc2626]/10 border border-[#dc2626]/30 flex items-center justify-center font-bold text-[#ff4b4b] shadow-[0_0_15px_rgba(220,38,38,0.2)]">2</div>
                <div>
                  <h2 className="text-2xl font-bold">Current Level</h2>
                </div>
              </div>
              <div className="grid grid-cols-3 gap-3 md:gap-4">
                {[
                  { id: "A1-A2", label: "Beginner", tag: "A1 - A2" },
                  { id: "B1-B2", label: "Intermediate", tag: "B1 - B2" },
                  { id: "C1-C2", label: "Advanced", tag: "C1 - C2" }
                ].map((lvl) => (
                  <button
                    key={lvl.id}
                    onClick={() => setLevel(lvl.id as Level)}
                    className={`relative p-4 md:p-5 rounded-2xl border transition-all duration-300 flex flex-col items-center justify-center gap-1 group ${
                      level === lvl.id 
                        ? "bg-[#102d56]/40 border-cyan-500/50 shadow-[0_10px_30px_rgba(16,45,86,0.3)]" 
                        : "bg-white/5 border-white/5 hover:border-white/15 hover:bg-white/10"
                    }`}
                  >
                    <span className={`text-[10px] uppercase font-black tracking-widest ${level === lvl.id ? 'text-cyan-400' : 'text-slate-500 group-hover:text-slate-300'}`}>{lvl.tag}</span>
                    <span className={`font-bold text-sm md:text-base ${level === lvl.id ? 'text-white' : 'text-slate-300'}`}>{lvl.label}</span>
                  </button>
                ))}
              </div>
            </motion.div>

            {/* 3. Study Format */}
            <motion.div initial={{ opacity: 0, x: -20 }} animate={{ opacity: 1, x: 0 }} transition={{ delay: 0.3 }}>
              <div className="flex items-center gap-4 mb-6">
                <div className="w-10 h-10 rounded-xl bg-[#dc2626]/10 border border-[#dc2626]/30 flex items-center justify-center font-bold text-[#ff4b4b] shadow-[0_0_15px_rgba(220,38,38,0.2)]">3</div>
                <div>
                  <h2 className="text-2xl font-bold">Study Format</h2>
                </div>
              </div>
              <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                {[
                  { id: "platform", label: "Platform Only", icon: MonitorPlay, desc: "Self-paced video LMS" },
                  { id: "individual", label: "1-on-1 Tutoring", icon: User, desc: "Private live sessions" },
                  { id: "group", label: "Group Classes", icon: Users, desc: "Interactive small groups" },
                  { id: "corporate", label: "Corporate", icon: Briefcase, desc: "B2B team training" }
                ].map((type) => {
                  const Icon = type.icon;
                  return (
                    <button
                      key={type.id}
                      onClick={() => setStudyType(type.id as StudyType)}
                      className={`relative p-5 rounded-2xl border transition-all duration-300 flex items-center gap-4 group ${
                        studyType === type.id 
                          ? "bg-[#dc2626]/10 border-[#dc2626] shadow-[0_10px_30px_rgba(220,38,38,0.15)]" 
                          : "bg-white/5 border-white/5 hover:border-white/15 hover:bg-white/10"
                      }`}
                    >
                      <div className={`w-12 h-12 rounded-xl flex items-center justify-center shrink-0 ${studyType === type.id ? 'bg-[#dc2626] text-white shadow-lg shadow-[#dc2626]/30' : 'bg-white/10 text-slate-400 group-hover:text-white'}`}>
                        <Icon className="w-6 h-6" />
                      </div>
                      <div className="text-left">
                        <div className="font-bold text-white tracking-wide">{type.label}</div>
                        <div className={`text-xs ${studyType === type.id ? 'text-[#ff4b4b]' : 'text-slate-400'}`}>{type.desc}</div>
                      </div>
                      {studyType === type.id && (
                        <div className="absolute top-4 right-4">
                          <CheckCircle2 className="w-5 h-5 text-[#dc2626]" />
                        </div>
                      )}
                    </button>
                  )
                })}
              </div>
            </motion.div>

            {/* 4. Duration (If applicable) */}
            <AnimatePresence mode="wait">
              {studyType !== "platform" && (
                <motion.div 
                  initial={{ opacity: 0, height: 0 }} 
                  animate={{ opacity: 1, height: "auto" }} 
                  exit={{ opacity: 0, height: 0 }} 
                  className="overflow-hidden"
                >
                  <div className="pt-4">
                    <div className="flex items-center gap-4 mb-6">
                      <div className="w-10 h-10 rounded-xl bg-[#dc2626]/10 border border-[#dc2626]/30 flex items-center justify-center font-bold text-[#ff4b4b] shadow-[0_0_15px_rgba(220,38,38,0.2)]">4</div>
                      <div>
                        <h2 className="text-2xl font-bold">Duration</h2>
                      </div>
                    </div>
                    <div className="grid grid-cols-3 gap-3 md:gap-4">
                      {[
                        { id: "1m", label: "1 Month", tag: "TRIAL" },
                        { id: "3m", label: "3 Months", tag: "POPULAR" },
                        { id: "6m", label: "6 Months", tag: "BEST VALUE" }
                      ].map((dur) => (
                        <button
                          key={dur.id}
                          onClick={() => setDuration(dur.id as Duration)}
                          className={`relative p-4 md:p-5 rounded-2xl border transition-all duration-300 flex flex-col items-center justify-center group ${
                            duration === dur.id 
                              ? "bg-[#102d56]/40 border-cyan-500/50 shadow-[0_10px_30px_rgba(16,45,86,0.3)]" 
                              : "bg-white/5 border-white/5 hover:border-white/15 hover:bg-white/10"
                          }`}
                        >
                          <span className={`font-bold text-sm md:text-base ${duration === dur.id ? 'text-white' : 'text-slate-300'}`}>{dur.label}</span>
                          <span className={`text-[10px] uppercase font-black tracking-widest mt-1 ${duration === dur.id ? 'text-cyan-400' : 'text-slate-500'}`}>{dur.tag}</span>
                        </button>
                      ))}
                    </div>
                  </div>
                </motion.div>
              )}
            </AnimatePresence>

            {/* Corporate Employees Count */}
            <AnimatePresence>
              {studyType === "corporate" && (
                <motion.div initial={{ opacity: 0, y: 10 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0, y: -10 }} className="p-6 bg-white/5 rounded-2xl border border-white/10 mt-6 backdrop-blur-md">
                  <label className="block font-bold text-slate-300 mb-3">Number of Employees (Minimum 10)</label>
                  <div className="flex items-center gap-4">
                    <input 
                      type="number" 
                      min="10" 
                      value={employeeCount}
                      onChange={(e) => setEmployeeCount(Math.max(10, parseInt(e.target.value) || 10))}
                      className="w-full max-w-[150px] bg-[#030408]/80 border border-white/20 rounded-xl px-4 py-3 text-white text-xl font-bold focus:outline-none focus:border-[#dc2626] focus:ring-1 focus:ring-[#dc2626] transition-all"
                    />
                    <span className="text-slate-400 font-medium">employees</span>
                  </div>
                </motion.div>
              )}
            </AnimatePresence>

          </div>

          {/* RIGHT COLUMN: SUMMARY BENTO BOX */}
          <motion.div initial={{ opacity: 0, scale: 0.95 }} animate={{ opacity: 1, scale: 1 }} transition={{ delay: 0.4 }} className="lg:col-span-4">
            <div className="sticky top-24 bg-[#0f111a]/80 backdrop-blur-2xl border border-white/10 rounded-3xl overflow-hidden shadow-[0_20px_50px_rgba(0,0,0,0.5)]">
              
              {/* Header Gradient */}
              <div className="bg-gradient-to-r from-[#7f1d1d] via-[#dc2626] to-[#ff4b4b] p-8 text-white relative overflow-hidden">
                <div className="absolute -top-10 -right-10 w-32 h-32 bg-white/20 rounded-full blur-2xl" />
                <div className="flex items-start gap-4 relative z-10">
                  <div className="bg-white/20 p-2.5 rounded-xl backdrop-blur-md shadow-inner border border-white/20">
                    <Globe2 className="w-6 h-6 text-white" />
                  </div>
                  <div>
                    <h3 className="text-2xl font-black mb-1">Your Summary</h3>
                    <p className="text-white/80 text-sm font-medium">Review your configuration</p>
                  </div>
                </div>
              </div>

              <div className="p-8">
                <div className="space-y-5 mb-8 text-sm">
                  <div className="flex justify-between items-center border-b border-white/5 pb-4">
                    <span className="text-slate-400 flex items-center gap-2"><Globe2 className="w-4 h-4"/> Language</span>
                    <span className="font-bold text-white flex items-center gap-2">
                      {language === "EN" ? "English" : language === "RO" ? "Romanian" : "Japanese"}
                      <img src={language === "EN" ? "https://flagcdn.com/w20/gb.png" : language === "RO" ? "https://flagcdn.com/w20/ro.png" : "https://flagcdn.com/w20/jp.png"} alt="flag" className="w-4 h-3 rounded-sm object-cover" />
                    </span>
                  </div>
                  
                  <div className="flex justify-between items-center border-b border-white/5 pb-4">
                    <span className="text-slate-400 flex items-center gap-2"><CheckCircle2 className="w-4 h-4"/> CEFR Level</span>
                    <span className="font-bold text-cyan-400">{level}</span>
                  </div>
                  
                  <div className="flex justify-between items-center border-b border-white/5 pb-4">
                    <span className="text-slate-400 flex items-center gap-2"><User className="w-4 h-4"/> Format</span>
                    <span className="font-bold text-white capitalize">{studyType.replace('-', ' ')}</span>
                  </div>

                  {studyType !== "platform" && (
                    <div className="flex justify-between items-center border-b border-white/5 pb-4">
                      <span className="text-slate-400">Duration</span>
                      <span className="font-bold text-white">{duration === "1m" ? "1 Month" : duration === "3m" ? "3 Months" : "6 Months"}</span>
                    </div>
                  )}

                  {studyType === "corporate" && (
                    <div className="flex justify-between items-center border-b border-white/5 pb-4">
                      <span className="text-slate-400">Employees</span>
                      <span className="font-bold text-white">{employeeCount}</span>
                    </div>
                  )}
                </div>

                <div className="mb-8 p-5 rounded-2xl bg-white/5 border border-white/5 shadow-inner">
                  <div className="flex justify-between items-end mb-1">
                    <span className="text-slate-400 font-bold uppercase tracking-wider text-xs">Total Investment</span>
                    <div className="text-right">
                      <span className="text-3xl font-black text-white">{totalPrice.toLocaleString()} <span className="text-lg text-[#dc2626]">RON</span></span>
                    </div>
                  </div>
                  <div className="text-right text-xs text-slate-500 font-medium">approx. €{Math.round(totalPrice / 4.97)}</div>
                </div>

                <ul className="space-y-3 mb-8 text-sm text-slate-400 font-medium">
                  <li className="flex items-center gap-3"><div className="w-5 h-5 rounded-full bg-cyan-500/20 flex items-center justify-center"><CheckCircle2 className="w-3 h-3 text-cyan-400" /></div> Full access to Rima LMS</li>
                  <li className="flex items-center gap-3"><div className="w-5 h-5 rounded-full bg-cyan-500/20 flex items-center justify-center"><CheckCircle2 className="w-3 h-3 text-cyan-400" /></div> Native certified tutors</li>
                  <li className="flex items-center gap-3"><div className="w-5 h-5 rounded-full bg-cyan-500/20 flex items-center justify-center"><CheckCircle2 className="w-3 h-3 text-cyan-400" /></div> Progress tracking & Certs</li>
                </ul>

                <button 
                  onClick={handleEnroll}
                  className="group relative w-full py-4 bg-gradient-to-r from-[#991b1b] to-[#dc2626] text-white rounded-xl font-bold flex items-center justify-center gap-2 overflow-hidden shadow-[0_10px_30px_rgba(220,38,38,0.3)] hover:shadow-[0_15px_40px_rgba(220,38,38,0.5)] transition-all duration-300"
                >
                  <div className="absolute inset-0 bg-white/20 translate-y-full group-hover:translate-y-0 transition-transform duration-300 ease-in-out" />
                  <span className="relative z-10 flex items-center gap-2">
                    ENROLL NOW <ArrowRight className="w-5 h-5 group-hover:translate-x-1 transition-transform" />
                  </span>
                </button>
                
                <div className="mt-6 flex justify-center gap-6 text-[10px] text-slate-500 font-bold uppercase tracking-wider">
                  <span className="flex items-center gap-1.5"><Shield className="w-3.5 h-3.5 text-green-500"/> Secure payment</span>
                  <span className="flex items-center gap-1.5"><Play className="w-3.5 h-3.5 text-cyan-500"/> Instant access</span>
                </div>
              </div>
            </div>
          </motion.div>

        </div>
      </div>
    </main>
  );
}
