import React from 'react';
import { useNavigate, useLocation, Link } from 'react-router-dom';
import {
  Home, Users, Disc, Radio, Settings, LogOut, ShoppingBag,
  GraduationCap, BarChart2, Shield, Box, MessageSquare, Cloud,
  Layers, Cpu, Heart, ListMusic, CreditCard, DollarSign, Terminal,
  ChevronLeft, ChevronRight,
} from 'lucide-react';
import { NavItem } from '../../types';
import { useAuthStore } from '@/features/auth/store/authStore';
import { useUIStore } from '@/stores/ui';
import { cn } from '@/lib/utils';
import { Button } from '@/components/ui/button';

interface SidebarProps {
  currentView?: string;
  onNavigate?: (viewId: string) => void;
  onLogout?: () => void;
}

const navItems: { section: string; items: NavItem[] }[] = [
  {
    section: 'My Studio',
    items: [
      { id: 'dashboard', label: 'Command Center', icon: <Home className="w-4 h-4" /> },
      { id: 'studio', label: 'Cloud Files', icon: <Cloud className="w-4 h-4" /> },
      { id: 'tracks', label: 'Projects', icon: <Layers className="w-4 h-4" /> },
      { id: 'gear', label: 'Gear Locker', icon: <Box className="w-4 h-4" /> },
      { id: 'analytics', label: 'Performance', icon: <BarChart2 className="w-4 h-4" /> },
    ],
  },
  {
    section: 'Veza Network',
    items: [
      { id: 'social', label: 'Community Feed', icon: <Users className="w-4 h-4" /> },
      { id: 'marketplace', label: 'Marketplace', icon: <ShoppingBag className="w-4 h-4" /> },
      { id: 'live', label: 'Live Sessions', icon: <Radio className="w-4 h-4 text-destructive" />, badge: 3 },
      { id: 'chat', label: 'Channels', icon: <MessageSquare className="w-4 h-4" />, badge: 12 },
      { id: 'education', label: 'Academy', icon: <GraduationCap className="w-4 h-4" /> },
    ],
  },
  {
    section: 'Commerce',
    items: [
      { id: 'sell', label: 'Seller Dashboard', icon: <DollarSign className="w-4 h-4" /> },
      { id: 'wishlist', label: 'Wishlist', icon: <Heart className="w-4 h-4" /> },
      { id: 'purchases', label: 'Purchases', icon: <CreditCard className="w-4 h-4" /> },
    ],
  },
  {
    section: 'Library',
    items: [
      { id: 'playlists', label: 'Playlists', icon: <ListMusic className="w-4 h-4" /> },
      { id: 'queue', label: 'Play Queue', icon: <Disc className="w-4 h-4" /> },
    ],
  },
  {
    section: 'System',
    items: [
      { id: 'developer', label: 'Developer API', icon: <Terminal className="w-4 h-4" /> },
      { id: 'admin', label: 'Admin Panel', icon: <Shield className="w-4 h-4" /> },
    ],
  },
];

const routeMap: Record<string, string> = {
  dashboard: '/dashboard', studio: '/library', tracks: '/library', gear: '/gear',
  analytics: '/analytics', social: '/social', marketplace: '/marketplace', live: '/live',
  chat: '/chat', education: '/education', sell: '/sell', wishlist: '/wishlist',
  purchases: '/purchases', playlists: '/playlists', queue: '/queue', developer: '/developer',
  admin: '/admin', settings: '/settings',
};

export const Sidebar: React.FC<SidebarProps> = ({ currentView, onNavigate, onLogout }) => {
  const navigate = useNavigate();
  const location = useLocation();
  const { logout } = useAuthStore();
  const { sidebarOpen, setSidebarOpen } = useUIStore();

  const handleMobileNav = () => {
    if (window.innerWidth < 1024) setSidebarOpen(false);
  };

  const activeView = currentView || Object.keys(routeMap).find((key) => routeMap[key] === location.pathname) || 'dashboard';

  const handleLogout = () => {
    logout();
    navigate('/login');
    onLogout?.();
  };

  return (
    <>
      {sidebarOpen && (
        <div
          className="fixed inset-0 bg-black/60 backdrop-blur-sm lg:hidden z-[90]"
          onClick={() => setSidebarOpen(false)}
          aria-hidden="true"
        />
      )}

      <aside
        className={cn(
          'fixed left-6 bottom-6 top-24 glass rounded-2xl flex flex-col transition-all duration-500 ease-[cubic-bezier(0.33,1,0.68,1)] z-[95] overflow-hidden border border-white/5',
          sidebarOpen ? 'w-64 translate-x-0 opacity-100' : '-translate-x-full lg:translate-x-0 lg:opacity-100 lg:w-20'
        )}
      >
        {/* Header */}
        <div className="p-6 border-b border-white/5 flex items-center gap-4 relative">
          <div className="w-8 h-8 rounded-lg bg-muted/20 flex items-center justify-center border border-white/10 shadow-[0_0_15px_rgba(0,0,0,0.5)] flex-shrink-0">
            <Cpu className="w-4 h-4 text-muted-foreground" />
          </div>

          <div className={cn("transition-all duration-300 overflow-hidden", sidebarOpen ? "w-auto opacity-100" : "w-0 opacity-0")}>
            <h2 className="text-xs font-display font-bold text-foreground tracking-widest uppercase whitespace-nowrap">
              System Hub
            </h2>
            <div className="flex items-center gap-1.5 mt-0.5">
              <span className="w-1.5 h-1.5 rounded-full bg-primary animate-pulse shadow-[0_0_8px_var(--color-primary)]" />
              <span className="text-[9px] font-mono text-primary opacity-90 uppercase whitespace-nowrap tracking-wider">
                Online
              </span>
            </div>
          </div>

          <Button
            variant="ghost"
            size="icon"
            onClick={() => setSidebarOpen(!sidebarOpen)}
            className={cn(
              "ml-auto text-muted-foreground hover:text-white hidden lg:flex hover:bg-white/5",
              !sidebarOpen && "absolute left-1/2 -translate-x-1/2 top-1/2 -translate-y-1/2"
            )}
          >
            {sidebarOpen ? <ChevronLeft className="w-4 h-4" /> : <ChevronRight className="w-4 h-4" />}
          </Button>
        </div>

        {/* Nav Content */}
        <div className="flex-1 overflow-y-auto custom-scrollbar p-3 space-y-6">
          {navItems.map((group, idx) => (
            <div key={idx}>
              <h3 className={cn(
                "text-[9px] font-mono font-bold text-muted-foreground/50 uppercase tracking-[0.2em] mb-2 px-3 transition-all duration-300",
                !sidebarOpen && "opacity-0 h-0 overflow-hidden mb-0"
              )}>
                {group.section}
              </h3>

              <div className="space-y-1">
                {group.items.map((item) => {
                  const route = routeMap[item.id] || '/dashboard';
                  const isActive = activeView === item.id;

                  return (
                    <Link
                      key={item.id}
                      to={route}
                      onClick={() => {
                        handleMobileNav();
                        onNavigate?.(item.id);
                      }}
                      className={cn(
                        'w-full flex items-center px-3 py-2.5 rounded-lg text-sm font-medium transition-all duration-200 group relative overflow-hidden',
                        isActive
                          ? 'bg-primary/10 text-foreground shadow-[inset_2px_0_0_0_var(--color-primary)]'
                          : 'text-muted-foreground hover:text-foreground hover:bg-muted',
                        !sidebarOpen && "justify-center px-0"
                      )}
                      title={!sidebarOpen ? item.label : undefined}
                    >
                      {/* Glow for active state */}
                      {isActive && (
                        <div className="absolute inset-0 bg-gradient-to-r from-primary/10 to-transparent opacity-50 pointer-events-none" />
                      )}

                      <div className={cn("flex items-center gap-3 relative z-10", !sidebarOpen && "justify-center")}>
                        <span className={cn(
                          'transition-colors duration-200',
                          isActive ? 'text-primary drop-shadow-[0_0_5px_rgba(var(--cyan-500),0.5)]' : 'group-hover:text-white'
                        )}>
                          {item.icon}
                        </span>

                        <span className={cn(
                          "transition-all duration-300 whitespace-nowrap origin-left font-sans text-[13px]",
                          sidebarOpen ? "w-auto opacity-100 scale-100" : "w-0 opacity-0 scale-95 hidden"
                        )}>
                          {item.label}
                        </span>
                      </div>

                      {/* Badges */}
                      {item.badge && sidebarOpen && (
                        <span className="ml-auto bg-secondary/20 text-secondary text-[9px] px-1.5 py-0.5 rounded font-mono font-bold border border-secondary/30 shadow-[0_0_10px_rgba(var(--magenta-500),0.2)]">
                          {item.badge}
                        </span>
                      )}

                      {item.badge && !sidebarOpen && (
                        <span className="absolute top-2 right-2 w-1.5 h-1.5 rounded-full bg-secondary shadow-[0_0_5px_var(--color-secondary)]" />
                      )}
                    </Link>
                  );
                })}
              </div>
            </div>
          ))}
        </div>

        {/* Footer */}
        <div className="p-3 border-t border-white/5 bg-black/20 backdrop-blur-sm">
          <Link
            to="/settings"
            onClick={() => { handleMobileNav(); onNavigate?.('settings'); }}
            className={cn(
              'flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm transition-all',
              activeView === 'settings' ? 'bg-primary/10 text-primary' : 'text-muted-foreground hover:text-foreground hover:bg-muted',
              !sidebarOpen && "justify-center"
            )}
          >
            <Settings className="w-4 h-4" />
            <span className={cn("transition-all duration-300 whitespace-nowrap", sidebarOpen ? "opacity-100" : "w-0 opacity-0 hidden")}>Settings</span>
          </Link>

          <Button
            variant="ghost"
            onClick={handleLogout}
            className={cn(
              "w-full text-destructive/80 hover:text-destructive hover:bg-destructive/10 mt-1 gap-3 justify-start",
              !sidebarOpen && "justify-center px-0"
            )}
          >
            <LogOut className="w-4 h-4" />
            <span className={cn("transition-all duration-300 whitespace-nowrap", sidebarOpen ? "opacity-100" : "w-0 opacity-0 hidden")}>Sign Out</span>
          </Button>
        </div>
      </aside>
    </>
  );
};
