"use client";

import { useState } from "react";
import Link from "next/link";
import {
  Compass,
  MessageCircle,
  PlusCircle,
  User,
  LogOut,
  ChevronDown,
  Sparkles,
  Bell,
  Search,
} from "lucide-react";

export default function AppLayout({ children }: { children: React.ReactNode }) {
  const [sidebarOpen, setSidebarOpen] = useState(true);
  const [userMenuOpen, setUserMenuOpen] = useState(false);
  const [activeTab, setActiveTab] = useState("discover");

  const navigationItems = [
    { id: "discover", label: "Discover", icon: Compass, href: "/app" },
    { id: "roleplay", label: "Roleplay", icon: Sparkles, href: "/app/roleplay" },
    { id: "create", label: "Create", icon: PlusCircle, href: "/app/create" },
    { id: "messages", label: "Messages", icon: MessageCircle, href: "/app/messages" },
    { id: "profile", label: "Profile", icon: User, href: "/app/profile" },
  ];

  return (
    <div className="h-screen flex flex-col bg-white">
      {/* Header */}
      <header className="border-b border-gray-200 sticky top-0 z-40 bg-white">
        <div className="px-4 sm:px-6 lg:px-8 py-4 flex justify-between items-center">
          {/* Logo + Title */}
          <Link href="/" className="flex items-center gap-2">
            <div className="w-8 h-8 bg-gradient-to-br from-pink-600 to-purple-600 rounded-lg flex items-center justify-center">
              <span className="text-white text-sm font-bold">RP</span>
            </div>
            <span className="font-semibold text-lg hidden sm:inline">RoleplayAI</span>
          </Link>

          {/* Center - Search Bar */}
          <div className="flex-1 max-w-md mx-4 hidden md:block">
            <div className="relative">
              <Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-5 h-5" />
              <input
                type="text"
                placeholder="Search characters..."
                className="w-full pl-10 pr-4 py-2 border border-gray-200 rounded-full text-sm focus:outline-none focus:border-black transition"
              />
            </div>
          </div>

          {/* Right - Actions */}
          <div className="flex items-center gap-4">
            {/* Subscribe Button */}
            <button className="hidden sm:block px-4 py-2 border-2 border-black rounded-full font-medium hover:bg-black hover:text-white transition text-sm">
              Subscribe
            </button>

            {/* Token Count */}
            <div className="flex items-center gap-2 px-4 py-2 bg-gray-50 rounded-full">
              <span className="text-sm font-semibold">⚡</span>
              <span className="text-sm font-medium">450</span>
            </div>

            {/* Notifications */}
            <button className="relative p-2 hover:bg-gray-100 rounded-full transition">
              <Bell className="w-5 h-5" />
              <span className="absolute top-1 right-1 w-2 h-2 bg-red-500 rounded-full"></span>
            </button>

            {/* User Avatar & Dropdown */}
            <div className="relative">
              <button
                onClick={() => setUserMenuOpen(!userMenuOpen)}
                className="flex items-center gap-2 p-1 hover:bg-gray-100 rounded-full transition"
              >
                <img
                  src="https://api.dicebear.com/7.x/avataaars/svg?seed=user"
                  alt="User"
                  className="w-8 h-8 rounded-full"
                />
                <ChevronDown className="w-4 h-4 text-gray-600" />
              </button>

              {/* Dropdown Menu */}
              {userMenuOpen && (
                <div className="absolute right-0 mt-2 w-56 bg-white border border-gray-200 rounded-xl shadow-lg z-50">
                  {/* Profile Section */}
                  <div className="p-4 border-b border-gray-200">
                    <div className="flex items-center gap-3">
                      <img
                        src="https://api.dicebear.com/7.x/avataaars/svg?seed=user"
                        alt="User"
                        className="w-10 h-10 rounded-full"
                      />
                      <div>
                        <p className="font-semibold text-sm">John Doe</p>
                        <p className="text-xs text-gray-500">@johndoe</p>
                      </div>
                    </div>
                  </div>

                  {/* Menu Items */}
                  <div className="py-2">
                    <Link href="/app/profile" className="block px-4 py-2 text-sm hover:bg-gray-50 transition">
                      My Profile
                    </Link>
                    <Link href="/app/settings" className="block px-4 py-2 text-sm hover:bg-gray-50 transition">
                      Settings
                    </Link>
                    <Link href="/app/billing" className="block px-4 py-2 text-sm hover:bg-gray-50 transition">
                      Billing & Plans
                    </Link>
                    <Link href="/app/favorites" className="block px-4 py-2 text-sm hover:bg-gray-50 transition">
                      Favorites
                    </Link>
                  </div>

                  {/* Divider */}
                  <div className="border-t border-gray-200"></div>

                  {/* Logout */}
                  <button className="w-full text-left px-4 py-2 text-sm text-red-600 hover:bg-red-50 transition flex items-center gap-2">
                    <LogOut className="w-4 h-4" />
                    Logout
                  </button>
                </div>
              )}
            </div>
          </div>
        </div>
      </header>

      {/* Main Content */}
      <div className="flex flex-1 overflow-hidden">
        {/* Sidebar */}
        <aside
          className={`${
            sidebarOpen ? "w-56" : "w-0"
          } border-r border-gray-200 bg-gray-50 flex flex-col transition-all duration-300 overflow-hidden`}
        >
          <nav className="flex-1 space-y-2 p-4">
            {navigationItems.map((item) => (
              <Link
                key={item.id}
                href={item.href}
                onClick={() => setActiveTab(item.id)}
                className={`flex items-center gap-3 px-4 py-3 rounded-lg font-medium text-sm transition ${
                  activeTab === item.id
                    ? "bg-white text-black shadow-sm"
                    : "text-gray-600 hover:bg-white/50"
                }`}
              >
                <item.icon className="w-5 h-5" />
                <span>{item.label}</span>
              </Link>
            ))}
          </nav>

          {/* Sidebar Footer */}
          <div className="border-t border-gray-200 p-4 space-y-3">
            <p className="text-xs text-gray-500 font-semibold">UPGRADE</p>
            <div className="bg-gradient-to-br from-pink-50 to-purple-50 rounded-lg p-3 space-y-2">
              <p className="text-xs font-semibold">Unlock Premium</p>
              <p className="text-xs text-gray-600">Get unlimited characters & faster responses</p>
              <button className="w-full bg-black text-white py-2 text-xs font-semibold rounded-lg hover:bg-gray-800 transition">
                Upgrade
              </button>
            </div>
          </div>
        </aside>

        {/* Main View */}
        <main className="flex-1 overflow-auto">
          {children}
        </main>
      </div>
    </div>
  );
}
