"use client";

import { useState } from "react";
import Link from "next/link";
import { User, ShoppingBag, MapPin, Download, Settings, LogOut, ChevronRight, Award, FileText, CheckCircle2, Camera, Plus, Cpu } from "lucide-react";

export default function MyAccountClient({ user, orders, certificates }: { user: any, orders: any[], certificates: any[] }) {
  const [activeTab, setActiveTab] = useState("dashboard");
  const [avatar, setAvatar] = useState(user?.image || `https://ui-avatars.com/api/?name=${user?.firstName || "U"}+${user?.lastName || "S"}&background=102d56&color=fff`);
  const [addresses, setAddresses] = useState([
    { id: 1, type: "Facturare", name: `${user?.firstName || "Nume"} ${user?.lastName || "Prenume"}`, street: "Str. Principală Nr. 10, Ap. 4", city: "București", country: "România", isDefault: true }
  ]);

  const tabs = [
    { id: "dashboard", label: "Panou de Control", icon: <User className="w-5 h-5" /> },
    { id: "orders", label: "Comenzi", icon: <ShoppingBag className="w-5 h-5" /> },
    { id: "certificates", label: "Diplome & Certificate", icon: <Award className="w-5 h-5" /> },
    { id: "addresses", label: "Adrese de Facturare", icon: <MapPin className="w-5 h-5" /> },
    { id: "account", label: "Detalii Cont", icon: <Settings className="w-5 h-5" /> },
  ];

  return (
    <div className="min-h-[100dvh] bg-[#030408] text-white pt-24 pb-32 px-6 relative font-sans">
      
      {/* Background Meshes */}
      <div className="absolute inset-0 pointer-events-none z-0">
        <div className="absolute top-[0%] left-[50%] -translate-x-1/2 w-[800px] h-[500px] bg-cyan-500/10 rounded-full 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 flex flex-col md:flex-row gap-8 lg:gap-12">
        
        {/* Sidebar Navigation */}
        <aside className="w-full md:w-64 shrink-0">
           <div className="mb-8 text-center md:text-left flex flex-col items-center md:items-start">
             <div className="relative mb-4 group cursor-pointer">
                <div className="w-24 h-24 rounded-full overflow-hidden border-2 border-white/10 group-hover:border-cyan-500/50 transition-colors relative">
                   <img src={avatar} alt="Avatar" className="w-full h-full object-cover" />
                   <div className="absolute inset-0 bg-black/50 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity">
                      <Camera className="w-6 h-6 text-white" />
                   </div>
                </div>
                {/* Hidden file input for actual upload implementation later */}
                <input type="file" className="hidden" accept="image/*" />
             </div>
             <h1 className="text-2xl font-black tracking-tight mb-1">{user?.firstName || "Student"} {user?.lastName || ""}</h1>
             <p className="text-slate-400 text-sm font-medium">{user?.email || "student@rima.ro"}</p>
           </div>
           
           <nav className="flex flex-col space-y-2">
             {tabs.map(tab => (
                <button
                  key={tab.id}
                  onClick={() => setActiveTab(tab.id)}
                  className={`flex items-center justify-between px-5 py-4 rounded-2xl font-bold text-sm transition-all ${
                    activeTab === tab.id 
                      ? "bg-white/10 text-cyan-400 border border-white/10 shadow-lg" 
                      : "text-slate-400 hover:bg-white/5 hover:text-white"
                  }`}
                >
                  <div className="flex items-center gap-3">
                    {tab.icon}
                    {tab.label}
                  </div>
                  {activeTab === tab.id && <ChevronRight className="w-4 h-4" />}
                </button>
             ))}
             
             <button className="flex items-center gap-3 px-5 py-4 rounded-2xl font-bold text-sm text-red-400 hover:bg-red-500/10 transition-colors mt-4">
                <LogOut className="w-5 h-5" />
                Ieșire din cont
             </button>
           </nav>
        </aside>

        {/* Content Area */}
        <main className="flex-1 bg-white/5 border border-white/10 rounded-3xl p-8 lg:p-12 backdrop-blur-md shadow-2xl min-h-[600px]">
          
          {/* Dashboard Tab */}
          {activeTab === "dashboard" && (
            <div className="animate-fade-in">
               <h2 className="text-2xl font-black mb-6">Salut, {user?.firstName || "Student"}!</h2>
               <p className="text-slate-400 text-base leading-relaxed mb-8 max-w-2xl">
                 Din panoul de control al contului poți vedea comenzile tale recente, poți gestiona adresele de facturare și livrare și îți poți edita parola și detaliile contului.
               </p>
               <div className="grid grid-cols-1 sm:grid-cols-3 gap-6">
                 <div className="bg-[#0f111a] p-6 rounded-2xl border border-white/5 flex flex-col gap-3">
                    <ShoppingBag className="w-8 h-8 text-cyan-400" />
                    <span className="font-bold text-lg">{orders.length}</span>
                    <span className="text-xs text-slate-500 uppercase font-bold tracking-widest">Comenzi Plasate</span>
                 </div>
                 <div className="bg-[#0f111a] p-6 rounded-2xl border border-white/5 flex flex-col gap-3">
                    <Award className="w-8 h-8 text-yellow-400" />
                    <span className="font-bold text-lg">{certificates.length}</span>
                    <span className="text-xs text-slate-500 uppercase font-bold tracking-widest">Diplome Obținute</span>
                 </div>
                 <div className="bg-[#0f111a] p-6 rounded-2xl border border-white/5 flex flex-col justify-center">
                    <Link href="/dashboard" className="text-cyan-400 font-bold hover:underline flex items-center gap-2">
                       Spre Cursuri <ChevronRight className="w-4 h-4" />
                    </Link>
                 </div>
               </div>
            </div>
          )}

          {/* Orders Tab */}
          {activeTab === "orders" && (
            <div className="animate-fade-in">
               <h2 className="text-2xl font-black mb-6 flex items-center gap-3">
                 <ShoppingBag className="w-6 h-6 text-cyan-400" /> Istoric Comenzi
               </h2>
               
               {orders.length === 0 ? (
                 <div className="bg-[#0f111a] border border-white/5 rounded-2xl p-8 text-center text-slate-400">
                    <ShoppingBag className="w-12 h-12 mx-auto mb-4 opacity-20" />
                    <p>Nu ai plasat nicio comandă încă.</p>
                 </div>
               ) : (
                 <div className="overflow-x-auto">
                   <table className="w-full text-left">
                     <thead>
                       <tr className="border-b border-white/10 text-xs uppercase tracking-widest text-slate-500">
                         <th className="pb-4 font-bold">Comanda #</th>
                         <th className="pb-4 font-bold">Data</th>
                         <th className="pb-4 font-bold">Status</th>
                         <th className="pb-4 font-bold">Total</th>
                         <th className="pb-4 font-bold text-right">Acțiuni</th>
                       </tr>
                     </thead>
                     <tbody className="divide-y divide-white/5">
                       {orders.map(order => (
                         <tr key={order.id} className="text-sm">
                           <td className="py-5 font-bold text-white">
                              {order.orderNumber}
                           </td>
                           <td className="py-5 text-slate-400">
                              {new Intl.DateTimeFormat('ro-RO').format(new Date(order.createdAt))}
                           </td>
                           <td className="py-5">
                              <span className={`px-2.5 py-1 rounded-md text-[10px] font-bold uppercase tracking-widest ${
                                order.status === 'COMPLETED' ? 'bg-green-500/10 text-green-400 border border-green-500/20' : 'bg-yellow-500/10 text-yellow-400 border border-yellow-500/20'
                              }`}>
                                {order.status}
                              </span>
                           </td>
                           <td className="py-5 font-bold text-white">
                              {order.totalAmount} {order.currency}
                           </td>
                           <td className="py-5 text-right">
                              <button className="text-cyan-400 font-bold hover:underline">Vizualizează</button>
                           </td>
                         </tr>
                       ))}
                     </tbody>
                   </table>
                 </div>
               )}
            </div>
          )}

          {/* Certificates Tab */}
          {activeTab === "certificates" && (
            <div className="animate-fade-in">
               <h2 className="text-2xl font-black mb-6 flex items-center gap-3">
                 <Award className="w-6 h-6 text-yellow-400" /> Certificate și Diplome
               </h2>
               
               {certificates.length === 0 ? (
                 <div className="bg-[#0f111a] border border-white/5 rounded-2xl p-8 text-center text-slate-400">
                    <Award className="w-12 h-12 mx-auto mb-4 opacity-20" />
                    <p>Nu ai obținut încă niciun certificat.</p>
                 </div>
               ) : (
                 <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                   {certificates.map(cert => (
                     <div key={cert.id} className="bg-[#0f111a] border border-white/10 rounded-2xl p-6 flex flex-col relative group overflow-hidden">
                        <div className="absolute top-0 right-0 p-4 opacity-10 group-hover:opacity-20 transition-opacity">
                          <Award className="w-24 h-24 text-yellow-500 -mt-6 -mr-6" />
                        </div>
                        <div className="relative z-10 flex-1">
                          <h3 className="font-bold text-lg text-white mb-1 line-clamp-1">{cert.course?.title || "Curs RIMA"}</h3>
                          <p className="text-xs text-slate-400 mb-6 font-medium">ID: {cert.certificateId}</p>
                          <div className="flex items-center gap-2 text-[10px] uppercase tracking-widest font-bold text-green-400 bg-green-500/10 w-fit px-2 py-1 rounded border border-green-500/20 mb-6">
                            <CheckCircle2 className="w-3 h-3" /> Valid
                          </div>
                        </div>
                        <Link 
                          href={`/certificates/${cert.id}`}
                          className="relative z-10 w-full py-3 bg-white/5 border border-white/10 hover:bg-white/10 rounded-xl flex items-center justify-center gap-2 font-bold text-sm transition-colors"
                        >
                          <FileText className="w-4 h-4 text-cyan-400" />
                          Vezi Certificat
                        </Link>
                     </div>
                   ))}
                 </div>
               )}
            </div>
          )}

          {/* Addresses Tab */}
          {activeTab === "addresses" && (
            <div className="animate-fade-in">
               <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4 mb-8">
                 <h2 className="text-2xl font-black flex items-center gap-3">
                   <MapPin className="w-6 h-6 text-cyan-400" /> Adrese de Facturare
                 </h2>
                 <button className="px-5 py-2.5 bg-white/5 border border-white/10 rounded-xl font-bold text-xs uppercase tracking-widest hover:bg-white/10 transition-colors flex items-center gap-2">
                   <Plus className="w-4 h-4" /> Adaugă Adresă
                 </button>
               </div>

               <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                 {addresses.map(address => (
                   <div key={address.id} className="relative aspect-[1.586/1] rounded-2xl p-6 overflow-hidden shadow-[0_20px_50px_rgba(0,0,0,0.5)] group cursor-pointer transition-transform hover:-translate-y-2">
                      {/* Card Background Gradient */}
                      <div className="absolute inset-0 bg-gradient-to-br from-[#1a1f35] to-[#0a0c14] z-0"></div>
                      <div className="absolute inset-0 bg-[url('https://www.transparenttextures.com/patterns/cubes.png')] opacity-10 z-0 mix-blend-overlay"></div>
                      
                      {/* Shine Effect */}
                      <div className="absolute -inset-[100%] bg-gradient-to-tr from-transparent via-white/5 to-transparent rotate-45 group-hover:animate-[shimmer_2s_infinite] z-0 pointer-events-none"></div>

                      <div className="relative z-10 flex flex-col h-full justify-between">
                         <div className="flex justify-between items-start">
                            <Cpu className="w-8 h-8 text-yellow-600/80" /> {/* Simulating Card Chip */}
                            <div className="text-[10px] font-bold uppercase tracking-widest text-slate-400 border border-white/10 px-2 py-1 rounded bg-black/20 backdrop-blur-md">
                               {address.type}
                            </div>
                         </div>

                         <div className="space-y-4">
                            {/* "Card Number" format for the Address lines */}
                            <div className="font-mono text-lg sm:text-xl tracking-widest text-white drop-shadow-md">
                               {address.street.substring(0, 16)}...
                            </div>
                            
                            <div className="flex justify-between items-end">
                               <div>
                                  <div className="text-[8px] uppercase tracking-widest text-slate-400 mb-1">Nume Titular</div>
                                  <div className="font-bold text-sm tracking-widest uppercase drop-shadow-md text-slate-200">
                                     {address.name}
                                  </div>
                               </div>
                               <div className="text-right">
                                  <div className="text-[8px] uppercase tracking-widest text-slate-400 mb-1">Locație</div>
                                  <div className="font-bold text-sm tracking-widest uppercase drop-shadow-md text-slate-200">
                                     {address.city}, {address.country.substring(0, 3)}
                                  </div>
                               </div>
                            </div>
                         </div>
                      </div>
                   </div>
                 ))}
                 
                 {/* Add New Card Placeholder */}
                 <div className="relative aspect-[1.586/1] rounded-2xl border-2 border-dashed border-white/10 flex flex-col items-center justify-center text-slate-400 hover:border-cyan-500/50 hover:text-cyan-400 hover:bg-white/5 transition-all cursor-pointer">
                    <Plus className="w-8 h-8 mb-2" />
                    <span className="font-bold text-sm uppercase tracking-widest">Adresă Nouă</span>
                 </div>
               </div>
            </div>
          )}

          {/* Account Tab */}
          {activeTab === "account" && (
            <div className="animate-fade-in">
               <h2 className="text-2xl font-black mb-6 flex items-center gap-3">
                 <Settings className="w-6 h-6 text-cyan-400" /> Detalii Cont
               </h2>
               
               <div className="flex flex-col md:flex-row gap-8 lg:gap-12 items-start">
                 <div className="w-full md:w-1/3 flex flex-col items-center justify-center bg-[#0f111a] border border-white/5 rounded-2xl p-6 relative">
                    <div className="w-32 h-32 rounded-full overflow-hidden border-4 border-white/5 relative mb-4 group cursor-pointer">
                       <img src={avatar} alt="Avatar" className="w-full h-full object-cover" />
                       <div className="absolute inset-0 bg-black/60 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity">
                          <Camera className="w-8 h-8 text-white" />
                       </div>
                    </div>
                    <button className="px-4 py-2 bg-white/5 hover:bg-white/10 border border-white/10 rounded-lg text-xs font-bold uppercase tracking-wider transition-colors w-full">
                       Încarcă Avatar Nou
                    </button>
                    <p className="text-[10px] text-slate-500 text-center mt-3">Format acceptat: JPG, PNG, GIF (Max 2MB)</p>
                 </div>

                 <form className="w-full md:w-2/3 space-y-6">
                  <div className="grid grid-cols-2 gap-4">
                    <div>
                      <label className="block text-xs font-bold text-slate-400 uppercase tracking-wider mb-2">Prenume</label>
                      <input type="text" defaultValue={user?.firstName} className="w-full bg-[#0f111a] border border-white/10 rounded-xl px-4 py-3 text-white focus:outline-none focus:border-cyan-500" />
                    </div>
                    <div>
                      <label className="block text-xs font-bold text-slate-400 uppercase tracking-wider mb-2">Nume</label>
                      <input type="text" defaultValue={user?.lastName} className="w-full bg-[#0f111a] border border-white/10 rounded-xl px-4 py-3 text-white focus:outline-none focus:border-cyan-500" />
                    </div>
                  </div>
                  <div>
                    <label className="block text-xs font-bold text-slate-400 uppercase tracking-wider mb-2">Adresă de Email</label>
                    <input type="email" defaultValue={user?.email} className="w-full bg-[#0f111a] border border-white/10 rounded-xl px-4 py-3 text-white focus:outline-none focus:border-cyan-500" />
                  </div>
                  <div className="pt-4">
                    <h3 className="text-lg font-bold mb-4">Schimbare Parolă</h3>
                    <div className="space-y-4">
                      <input type="password" placeholder="Parola Curentă" className="w-full bg-[#0f111a] border border-white/10 rounded-xl px-4 py-3 text-white focus:outline-none focus:border-cyan-500" />
                      <input type="password" placeholder="Parola Nouă" className="w-full bg-[#0f111a] border border-white/10 rounded-xl px-4 py-3 text-white focus:outline-none focus:border-cyan-500" />
                    </div>
                  </div>
                  <button type="button" className="px-8 py-3 bg-cyan-500 text-white font-bold rounded-xl hover:bg-cyan-400 transition-colors w-full sm:w-auto">
                    Salvează Modificările
                  </button>
               </form>
               </div>
            </div>
          )}

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