"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { signOut } from "next-auth/react";
import { 
  LayoutDashboard, 
  BookOpen, 
  Users, 
  BarChart3, 
  Building2, 
  Award, 
  LogOut, 
  Settings,
  ExternalLink,
  Bot,
  MessageSquare,
  Activity
} from "lucide-react";
import Image from "next/image";

const sidebar = [
  {
    section: "OVERVIEW",
    items: [
      { href: "/admin", label: "Panou general", icon: LayoutDashboard }
    ]
  },
  {
    section: "ACADEMY",
    items: [
      { href: "/admin/users", label: "Cursanți", icon: Users },
      { href: "/admin/courses", label: "Cursuri", icon: BookOpen },
      { href: "/admin/groups", label: "Grupe", icon: Users },
      { href: "/admin/orders", label: "Înscrieri & Comenzi", icon: BookOpen },
    ]
  },
  {
    section: "COMMUNICATION",
    items: [
      { href: "/admin/messages", label: "Mesaje", icon: MessageSquare }
    ]
  },
  {
    section: "CERTIFICATION",
    items: [
      { href: "/admin/certificates", label: "Certificate", icon: Award }
    ]
  },
  {
    section: "B2B & FINANCE",
    items: [
      { href: "/admin/b2b", label: "Companii B2B", icon: Building2 },
      { href: "/admin/reports", label: "Rapoarte Financiare", icon: BarChart3 }
    ]
  },
  {
    section: "ANALYTICS",
    items: [
      { href: "/admin/log", label: "Activitate", icon: Activity }
    ]
  }
];

export default function AdminLayout({ children }: { children: React.ReactNode }) {
  const pathname = usePathname();

  return (
    <div className="flex h-screen bg-[#090B14] text-[#f8fafc] overflow-hidden selection:bg-blue-500/30 font-sans relative">
      
      {/* Background Orbs from Plugin */}
      <div className="fixed -top-[200px] -right-[100px] w-[600px] h-[600px] rounded-full bg-[radial-gradient(circle,rgba(139,92,246,0.06)_0%,transparent_60%)] pointer-events-none z-0"></div>
      <div className="fixed -bottom-[200px] -left-[200px] w-[700px] h-[700px] rounded-full bg-[radial-gradient(circle,rgba(59,130,246,0.05)_0%,transparent_60%)] pointer-events-none z-0"></div>

      {/* Sidebar (matching --rima-sidebar-bg) */}
      <aside className="w-[260px] bg-[#0B0E17] border-r border-white/[0.06] flex flex-col hidden lg:flex shrink-0 z-50 relative">
        
        {/* Brand Header */}
        <div className="h-[70px] flex items-center px-6 border-b border-white/[0.06]">
          <Link href="/admin" className="flex items-center gap-3">
             {/* Replace with actual logo if needed */}
             <div className="w-8 h-8 bg-blue-500 rounded-md flex items-center justify-center">
                <span className="font-bold text-white tracking-tighter text-sm">RIMA</span>
             </div>
             <span className="font-bold text-sm tracking-widest text-white uppercase">Academy</span>
          </Link>
        </div>

        {/* Navigation */}
        <nav className="flex-1 py-6 px-4 space-y-6 overflow-y-auto custom-scrollbar">
          {sidebar.map((group) => (
             <div key={group.section} className="flex flex-col gap-1">
                <div className="text-[11px] font-bold text-[#94a3b8] uppercase tracking-[1px] px-3 mb-2">
                  {group.section}
                </div>
                {group.items.map((item) => {
                  const isActive = pathname === item.href || (item.href !== "/admin" && pathname.startsWith(item.href));
                  
                  return (
                    <Link 
                      key={item.href} 
                      href={item.href}
                      className={`flex items-center gap-3 px-3 py-2.5 rounded-lg transition-all duration-200 text-sm relative ${
                        isActive 
                        ? "bg-purple-500/10 text-white font-semibold" 
                        : "text-[#94a3b8] font-medium hover:text-white hover:bg-[#121624]"
                      }`}
                    >
                      {isActive && (
                         <div className="absolute left-0 top-1/2 -translate-y-1/2 h-[60%] w-1 bg-[#8b5cf6] rounded-r shadow-[0_0_10px_#8b5cf6]"></div>
                      )}
                      <item.icon className={`w-[18px] h-[18px] ${isActive ? "text-[#8b5cf6]" : "opacity-80"}`} />
                      <span>{item.label}</span>
                    </Link>
                  );
                })}
             </div>
          ))}
        </nav>

        {/* User Footer */}
        <div className="p-4 border-t border-white/[0.06] flex flex-col gap-1">
          <Link href="/admin/settings" className="flex items-center gap-3 px-3 py-2.5 rounded-lg text-[#94a3b8] hover:text-white hover:bg-[#121624] transition-all text-sm font-medium">
             <Settings className="w-[18px] h-[18px] opacity-80" /> Setări
          </Link>
          <button 
            onClick={() => signOut({ callbackUrl: "/login" })}
            className="flex items-center gap-3 px-3 py-2.5 w-full rounded-lg text-[#94a3b8] hover:text-red-400 hover:bg-red-400/10 transition-all text-sm font-medium"
          >
            <LogOut className="w-[18px] h-[18px] opacity-80" /> Deconectare
          </button>
        </div>
      </aside>

      {/* Main Content Area */}
      <main className="flex-1 flex flex-col min-w-0 z-10 relative">
        
        {/* Header (matching ra-header) */}
        <header className="h-[70px] bg-[rgba(9,11,20,0.6)] backdrop-blur-[20px] border-b border-white/[0.06] sticky top-0 z-40 flex items-center justify-between px-8">
          <div className="flex items-center gap-4">
             <div className="text-sm font-medium text-[#94a3b8]">
                RIMA Academy <span className="mx-2 opacity-50">/</span> <span className="text-white font-semibold">Control Panel</span>
             </div>
          </div>

          <div className="flex items-center gap-5">
             <div className="flex items-center gap-1.5 px-3 py-1.5 rounded-full bg-emerald-500/10 text-emerald-500 border border-emerald-500/20 text-xs font-semibold">
                <div className="w-2 h-2 rounded-full bg-emerald-500 shadow-[0_0_8px_#10b981]"></div> Live
             </div>

             <div className="flex items-center gap-3 border-r border-white/[0.06] pr-4">
                <Link href="/" target="_blank" className="w-9 h-9 rounded-full flex items-center justify-center text-[#94a3b8] hover:text-white hover:bg-[#121624] transition-all border border-transparent hover:border-white/[0.06]" title="Deschide site">
                   <ExternalLink className="w-4 h-4" />
                </Link>
             </div>

             <div className="flex items-center gap-2.5 pl-2">
                <div className="flex flex-col text-right">
                   <span className="text-[13px] font-semibold text-white">Administrator</span>
                   <span className="text-[11px] text-[#94a3b8]">Cont RIMA</span>
                </div>
                <div className="w-9 h-9 rounded-full bg-[#121624] border-2 border-[#191E30] flex items-center justify-center overflow-hidden">
                   <Users className="w-4 h-4 text-[#94a3b8]" />
                </div>
             </div>
          </div>
        </header>

        {/* Dynamic Page Content */}
        <div className="flex-1 overflow-y-auto p-8 custom-scrollbar">
           {children}
        </div>
      </main>
    </div>
  );
}
