import { A, useLocation } from '@solidjs/router';
import { For, Show } from 'solid-js';
import {
  LayoutGrid, Building2, Briefcase, Users, ShieldCheck, FileText,
  LayoutDashboard, ClipboardList, UserRoundSearch, UserCircle,
  Camera, Palette, BookOpen, Code2, BriefcaseBusiness, HandHelping,
  WalletCards, CreditCard, Tag, Percent, Receipt, ShoppingCart,
  FileCheck, Star, HeadphonesIcon, BarChart3,
  ChevronLeft, BadgeCheck, Activity, Film, Utensils, PenTool,
  Megaphone,
} from 'lucide-solid';

type NavItem = {
  href: string;
  label: string;
  icon: any;
  aliasPrefix?: string;
};

const GROUPS: NavItem[][] = [
  [
    { href: '/admin', label: 'Dashboard', icon: LayoutGrid },
  ],
  [
    { href: '/admin/department', label: 'Department Management', icon: Building2 },
    { href: '/admin/designation', label: 'Designation Management', icon: Briefcase },
    { href: '/admin/roles', label: 'Internal Role Management', icon: ShieldCheck },
    { href: '/admin/employees', label: 'Employee Management', icon: Users },
  ],
  [
    { href: '/admin/runtime-roles', label: 'External Role Management', icon: ShieldCheck },
    { href: '/admin/onboarding-management', label: 'External Onboarding Management', icon: FileText, aliasPrefix: '/admin/onboarding-schemas' },
    { href: '/admin/internal-dashboard-management', label: 'Internal Dashboard Management', icon: LayoutDashboard },
    { href: '/admin/external-dashboard-management', label: 'External Dashboard Management', icon: LayoutDashboard, aliasPrefix: '/admin/role-ui-configs' },
  ],
  [
    { href: '/admin/verification-status', label: 'Verification Management', icon: BadgeCheck },
    { href: '/admin/approval', label: 'Approval Management', icon: ClipboardList },
  ],
  [
    { href: '/admin/users', label: 'Users Management', icon: UserRoundSearch },
    { href: '/admin/company', label: 'Company Management', icon: Building2 },
    { href: '/admin/candidate', label: 'Candidate Management', icon: UserCircle },
    { href: '/admin/customer', label: 'Customer Management', icon: UserCircle },
  ],
  [
    { href: '/admin/photographer', label: 'Photographer Management', icon: Camera },
    { href: '/admin/makeup-artist', label: 'Makeup Artist Management', icon: Palette },
    { href: '/admin/tutors', label: 'Tutors Management', icon: BookOpen },
    { href: '/admin/developers', label: 'Developers Management', icon: Code2 },
    { href: '/admin/video-editors', label: 'Video Editor Management', icon: Film },
    { href: '/admin/fitness-trainers', label: 'Fitness Trainer Management', icon: Activity },
    { href: '/admin/catering-services', label: 'Catering Services Management', icon: Utensils },
    { href: '/admin/graphic-designers', label: 'Graphics Designer Management', icon: PenTool },
    { href: '/admin/social-media-managers', label: 'Social Media Manager Management', icon: Megaphone },
  ],
  [
    { href: '/admin/jobs', label: 'Jobs Management', icon: BriefcaseBusiness },
    { href: '/admin/leads', label: 'Leads Management', icon: HandHelping },
  ],
  [
    { href: '/admin/pricing', label: 'Pricing Management', icon: WalletCards },
    { href: '/admin/credit', label: 'Credit Management', icon: CreditCard },
    { href: '/admin/coupon', label: 'Coupon Management', icon: Tag },
    { href: '/admin/discount', label: 'Discount Management', icon: Percent },
    { href: '/admin/tax', label: 'Tax Management', icon: Receipt },
    { href: '/admin/order', label: 'Order Management', icon: ShoppingCart },
    { href: '/admin/invoice', label: 'Invoice Management', icon: FileCheck },
  ],
  [
    { href: '/admin/review', label: 'Review Management', icon: Star },
    { href: '/admin/support', label: 'Support Management', icon: HeadphonesIcon },
    { href: '/admin/report', label: 'Report Management', icon: BarChart3 },
    { href: '/admin/ledger', label: 'Ledger Management', icon: Receipt },
  ],
];

export default function AdminSidebar(props: {
  collapsed: boolean;
  onToggle: () => void;
  onNavigate?: () => void;
  adminName: string;
  adminInitials: string;
}) {
  const location = useLocation();

  const isActive = (item: NavItem) => {
    if (location.pathname === '/admin') return item.href === '/admin';
    if (item.href === '/admin') return false;
    if (item.aliasPrefix && location.pathname.startsWith(item.aliasPrefix)) return true;
    return location.pathname === item.href || location.pathname.startsWith(`${item.href}/`);
  };

  return (
    <aside
      style="overflow:hidden;display:flex;flex-direction:column;background:white;border-right:1px solid #E5E7EB;transition:width 0.3s;flex-shrink:0"
      style:width={props.collapsed ? '64px' : '220px'}
    >
      {/* Logo area */}
      <div style="position:relative;height:64px;display:flex;align-items:center;border-bottom:1px solid #E5E7EB;flex-shrink:0;padding:0 14px">
        <A href="/admin" onClick={props.onNavigate} style="display:flex;align-items:center;gap:10px;text-decoration:none;overflow:hidden">
          <Show
            when={!props.collapsed}
            fallback={
              <img src="/nxtgauge-icon.png" alt="Nxtgauge" style="width:32px;height:32px;object-fit:contain;flex-shrink:0" />
            }
          >
            <img src="/nxtgauge-logo.png" alt="Nxtgauge" style="height:28px;object-fit:contain;flex-shrink:0" />
          </Show>
        </A>
        <button
          type="button"
          onClick={props.onToggle}
          style="position:absolute;right:-10px;top:50%;transform:translateY(-50%);width:20px;height:20px;border-radius:50%;border:1px solid #E5E7EB;background:white;box-shadow:0 1px 4px rgba(0,0,0,0.1);display:flex;align-items:center;justify-content:center;cursor:pointer;z-index:10;color:#6B7280"
          aria-label={props.collapsed ? 'Expand sidebar' : 'Collapse sidebar'}
        >
          <ChevronLeft size={11} style={`transition:transform 0.3s;${props.collapsed ? 'transform:rotate(180deg)' : ''}`} />
        </button>
      </div>

      {/* Navigation */}
      <nav style="flex:1;min-height:0;overflow-y:auto;padding:10px 8px">
        <For each={GROUPS}>
          {(group, gi) => (
            <>
              <Show when={gi() > 0}>
                <div style="height:1px;background:#F3F4F6;margin:6px 4px" />
              </Show>
              <div style="display:flex;flex-direction:column;gap:1px">
                <For each={group}>
                  {(item) => {
                    const active = () => isActive(item);
                    const Icon = item.icon;
                    return (
                      <A
                        href={item.href}
                        onClick={props.onNavigate}
                        title={props.collapsed ? item.label : undefined}
                        style={`display:flex;align-items:center;height:36px;border-radius:8px;text-decoration:none;padding:0 ${props.collapsed ? '0' : '10px'};${props.collapsed ? 'justify-content:center;' : ''}${active() ? 'background:#FFF3EE;color:#FF5E13;' : 'color:#6B7280;'}`}
                        aria-current={active() ? 'page' : undefined}
                      >
                        <Icon
                          size={16}
                          style={`flex-shrink:0;${active() ? 'color:#FF5E13' : 'color:#9CA3AF'}`}
                          strokeWidth={active() ? 2.5 : 2}
                        />
                        <Show when={!props.collapsed}>
                          <span style="margin-left:9px;font-size:12.5px;font-weight:500;overflow:hidden;text-overflow:ellipsis;white-space:nowrap">{item.label}</span>
                        </Show>
                      </A>
                    );
                  }}
                </For>
              </div>
            </>
          )}
        </For>
      </nav>
    </aside>
  );
}
