import { A } from '@solidjs/router';
import AdminShell from '~/components/AdminShell';
import {
  Users, Building2, Briefcase, Clock, Ticket, Receipt,
  Package, Megaphone, Shield, UserCog, FormInput, LayoutDashboard,
  BadgeCheck, ArrowRight,
} from 'lucide-solid';
import type { Component } from 'solid-js';

type KpiCard = {
  label: string;
  value: string;
  badge?: string;
  badgeColor: string;
  href: string;
  icon: Component<any>;
};

const KPI: KpiCard[] = [
  { label: 'Total Users',       value: '—', badge: '+0%',             badgeColor: 'text-emerald-700 bg-emerald-50', href: '/admin/users',         icon: Users    },
  { label: 'Total Revenue',     value: '—', badge: 'Weekly',          badgeColor: 'text-[#0a1d37] bg-slate-100',   href: '/admin/invoice',       icon: Receipt  },
  { label: 'Active Roles',      value: '—', badge: 'Live',            badgeColor: 'text-amber-700 bg-amber-50',    href: '/admin/runtime-roles', icon: Briefcase},
  { label: 'Pending Approvals', value: '—', badge: 'Action Required', badgeColor: 'text-white bg-red-500',         href: '/admin/approval',      icon: Clock    },
];

const ACTIVITY = [
  { day: 'MON', h: 40 }, { day: 'TUE', h: 65 }, { day: 'WED', h: 95 },
  { day: 'THU', h: 70 }, { day: 'FRI', h: 55 }, { day: 'SAT', h: 30 }, { day: 'SUN', h: 45 },
];

const QUICK_ACTIONS = [
  { label: 'Create New Role',     href: '/admin/roles/create',                  icon: Shield        },
  { label: 'Configure Dashboard', href: '/admin/external-dashboard-management', icon: LayoutDashboard },
  { label: 'Add Employee',        href: '/admin/employees',                     icon: Users         },
];

const PIPELINE = [
  { name: 'Candidate Roles',  type: 'External Role',  status: 'Active',  statusColor: 'text-emerald-700 bg-emerald-50', progress: 85, barColor: 'bg-[#0a1d37]'  },
  { name: 'Onboarding Flows', type: 'Schema Builder', status: 'Pending', statusColor: 'text-amber-700 bg-amber-50',     progress: 42, barColor: 'bg-amber-400'   },
  { name: 'Dashboard Config', type: 'UI Config',      status: 'Draft',   statusColor: 'text-slate-500 bg-slate-100',    progress: 12, barColor: 'bg-slate-300'   },
];

const CONTROL: Array<{ label: string; href: string; desc: string; icon: Component<any>; iconBg: string; iconFg: string }> = [
  { label: 'Internal Roles',      href: '/admin/roles',                         desc: 'Permissions & access levels for internal staff.',    icon: Shield,          iconBg: 'bg-blue-50',   iconFg: 'text-blue-600'   },
  { label: 'External Roles',      href: '/admin/runtime-roles',                 desc: 'Modules, credits & capabilities per external role.', icon: UserCog,         iconBg: 'bg-violet-50', iconFg: 'text-violet-600' },
  { label: 'Onboarding Flows',    href: '/admin/onboarding-schemas',            desc: 'Schema-driven onboarding forms per external role.',  icon: FormInput,       iconBg: 'bg-amber-50',  iconFg: 'text-amber-600'  },
  { label: 'External Dashboards', href: '/admin/external-dashboard-management', desc: 'Sidebar, widgets & runtimeConfig per role.',         icon: LayoutDashboard, iconBg: 'bg-teal-50',   iconFg: 'text-teal-600'   },
  { label: 'Internal Dashboards', href: '/admin/internal-dashboard-management', desc: 'Home widgets & KPI panels for internal staff.',      icon: LayoutDashboard, iconBg: 'bg-sky-50',    iconFg: 'text-sky-600'    },
  { label: 'Approval Queue',      href: '/admin/approval',                      desc: 'Review, approve or reject pending action requests.', icon: BadgeCheck,      iconBg: 'bg-rose-50',   iconFg: 'text-rose-600'   },
];

export default function AdminDashboard() {
  return (
    <AdminShell>
      <div class="space-y-6">

        {/* ── KPI Cards ── */}
        <section class="rounded-xl border border-slate-200/70 bg-white p-5 shadow-[0_4px_16px_-8px_rgba(10,29,55,0.18)]">
          <div class="mb-4 flex items-center justify-between">
            <h2 class="text-[15px] font-black tracking-tight text-[#0a1d37]">Quick Stats</h2>
            <span class="rounded-md bg-[#0a1d37]/8 px-2.5 py-1 text-[10px] font-bold uppercase tracking-[0.14em] text-[#0a1d37]">Live Overview</span>
          </div>
          <div class="grid grid-cols-1 gap-3 sm:grid-cols-2 xl:grid-cols-4">
          {KPI.map((kpi) => {
            const Icon = kpi.icon;
            return (
              <A
                href={kpi.href}
                class="relative overflow-hidden rounded-lg border border-slate-200/70 bg-[#f9fafc] p-4 transition-all duration-200 hover:-translate-y-px hover:border-[#0a1d37]/20 hover:shadow-[0_8px_20px_-12px_rgba(10,29,55,0.35)]"
              >
                {/* Left navy accent bar */}
                <div class="absolute left-0 top-0 h-full w-1 rounded-l-lg bg-[#0a1d37]" />

                <div class="flex items-start justify-between pl-2">
                  <div class="flex h-8 w-8 items-center justify-center rounded-md bg-[#0a1d37]/8">
                    <Icon class="h-4 w-4 text-[#0a1d37]" />
                  </div>
                  {kpi.badge && (
                    <span class={`rounded px-1.5 py-0.5 text-[10px] font-bold ${kpi.badgeColor}`}>
                      {kpi.badge}
                    </span>
                  )}
                </div>

                <div class="mt-3 pl-2">
                  <h3 class="text-[10px] font-bold uppercase tracking-widest text-slate-400">
                    {kpi.label}
                  </h3>
                  <p class="mt-1 text-2xl font-black tabular-nums tracking-tight text-[#0a1d37]">
                    {kpi.value}
                  </p>
                </div>
              </A>
            );
          })}
          </div>
        </section>

        {/* ── Activity + Intelligence Hub ── */}
        <div class="grid grid-cols-1 gap-4 lg:grid-cols-3">

          {/* Activity Chart */}
          <div class="lg:col-span-2 rounded-xl border border-slate-200/70 bg-white p-5 shadow-[0_4px_16px_-8px_rgba(10,29,55,0.18)]">
            <div class="mb-5 flex items-end justify-between">
              <div>
                <h2 class="text-[15px] font-black tracking-tight text-[#0a1d37]">System Activity</h2>
                <p class="mt-0.5 text-[12px] text-slate-400">Platform traffic & Tracecoin velocity</p>
              </div>
              <select class="rounded-md border border-slate-200 bg-white py-1.5 pl-3 pr-7 text-[11px] font-semibold text-[#0a1d37] focus:outline-none focus:ring-2 focus:ring-[#0a1d37]/10">
                <option>Last 7 Days</option>
                <option>Last 30 Days</option>
              </select>
            </div>
            <div class="flex h-40 items-end justify-between gap-2">
              {ACTIVITY.map((d) => (
                <div class="group flex flex-1 flex-col items-center gap-1.5">
                  <div
                    class={`w-full rounded-t transition-all duration-200 ${d.day === 'WED' ? 'bg-[#0a1d37] shadow-md shadow-[#0a1d37]/20' : 'bg-slate-100 group-hover:bg-slate-200'}`}
                    style={`height: ${d.h}%`}
                  />
                  <span class={`text-[10px] font-bold ${d.day === 'WED' ? 'text-[#0a1d37]' : 'text-slate-400'}`}>
                    {d.day}
                  </span>
                </div>
              ))}
            </div>
          </div>

          {/* Intelligence Hub + Pipeline */}
          <div class="space-y-3">
            {/* Intelligence Hub */}
            <div class="relative overflow-hidden rounded-xl bg-[#0a1d37] p-4 text-white shadow-[0_8px_24px_-10px_rgba(10,29,55,0.55)]">
              <div class="absolute -right-4 -top-4 h-20 w-20 rounded-full bg-white/5 blur-2xl" />
              <h2 class="mb-3 text-[13px] font-black">Intelligence Hub</h2>
              <div class="space-y-1.5">
                {QUICK_ACTIONS.map((a) => {
                  const Icon = a.icon;
                  return (
                    <A
                      href={a.href}
                      class="group flex w-full items-center justify-between rounded-md bg-white/10 px-3 py-2.5 transition-all hover:bg-white/15"
                    >
                      <span class="text-[12.5px] font-semibold">{a.label}</span>
                      <Icon class="h-3.5 w-3.5 text-white/60 transition-transform group-hover:translate-x-0.5" />
                    </A>
                  );
                })}
              </div>
            </div>

            {/* Pipeline Status */}
            <div class="rounded-xl border border-slate-200/70 bg-white p-4 shadow-[0_4px_16px_-8px_rgba(10,29,55,0.18)]">
              <h2 class="mb-3 text-[10px] font-bold uppercase tracking-widest text-[#0a1d37]">
                Pipeline Status
              </h2>
              <div class="space-y-3.5">
                {PIPELINE.map((p) => (
                  <div>
                    <div class="mb-1.5 flex items-center justify-between gap-2">
                      <div class="min-w-0">
                        <p class="truncate text-[12.5px] font-bold text-[#0a1d37]">{p.name}</p>
                        <p class="text-[10px] text-slate-400">{p.type}</p>
                      </div>
                      <span class={`shrink-0 rounded px-1.5 py-0.5 text-[10px] font-bold uppercase ${p.statusColor}`}>
                        {p.status}
                      </span>
                    </div>
                    <div class="h-1 w-full overflow-hidden rounded-full bg-slate-100">
                      <div class={`h-full rounded-full ${p.barColor}`} style={`width: ${p.progress}%`} />
                    </div>
                  </div>
                ))}
              </div>
            </div>
          </div>
        </div>

        {/* ── Control Plane ── */}
        <div>
          <div class="mb-3 flex items-center justify-between">
            <h2 class="text-[15px] font-black tracking-tight text-[#0a1d37]">Control Plane</h2>
            <span class="rounded border border-slate-200 bg-white px-2 py-0.5 text-[10px] font-bold uppercase tracking-widest text-slate-400">
              {CONTROL.length} modules
            </span>
          </div>
          <div class="grid grid-cols-1 gap-3 sm:grid-cols-2 lg:grid-cols-3">
            {CONTROL.map((item) => {
              const Icon = item.icon;
              return (
                <A
                  href={item.href}
                  class="group flex items-start gap-3 rounded-lg border border-slate-200/70 bg-white p-3.5 shadow-[0_2px_8px_-4px_rgba(10,29,55,0.18)] transition-all duration-200 hover:-translate-y-px hover:border-[#0a1d37]/20 hover:shadow-[0_8px_20px_-8px_rgba(10,29,55,0.28)]"
                >
                  <div class={`mt-0.5 flex h-7 w-7 shrink-0 items-center justify-center rounded-md ${item.iconBg}`}>
                    <Icon class={`h-3.5 w-3.5 ${item.iconFg}`} />
                  </div>
                  <div class="min-w-0 flex-1">
                    <p class="truncate text-[13px] font-bold text-[#0a1d37] transition-colors group-hover:text-[#0a1d37]">{item.label}</p>
                    <p class="mt-0.5 text-[11px] leading-relaxed text-slate-400">{item.desc}</p>
                  </div>
                  <ArrowRight class="mt-0.5 h-3.5 w-3.5 shrink-0 text-slate-200 transition-all group-hover:translate-x-0.5 group-hover:text-[#0a1d37]" />
                </A>
              );
            })}
          </div>
        </div>

      </div>
    </AdminShell>
  );
}
