import { createFileRoute, Link } from "@tanstack/react-router";
import { useState } from "react";
import { LayoutDashboard, Users, ListChecks, FileCheck2, Wallet, Gift, Settings, ScrollText, ShieldAlert, ArrowLeft } from "lucide-react";
import { useMe } from "@/lib/app";
import { AdminOverview } from "@/components/admin/AdminOverview";
import { AdminUsers } from "@/components/admin/AdminUsers";
import { AdminTasks } from "@/components/admin/AdminTasks";
import { AdminSubmissions } from "@/components/admin/AdminSubmissions";
import { AdminWithdrawals } from "@/components/admin/AdminWithdrawals";
import { AdminReferrals } from "@/components/admin/AdminReferrals";
import { AdminSettings } from "@/components/admin/AdminSettings";
import { AdminLogs } from "@/components/admin/AdminLogs";

export const Route = createFileRoute("/_authenticated/admin")({
  head: () => ({ meta: [{ title: "Admin Panel – RozTask" }, { name: "description", content: "Manage users, tasks and payouts." }, { name: "robots", content: "noindex" }, { property: "og:title", content: "Admin Panel – RozTask" }, { property: "og:description", content: "Manage users, tasks and payouts." }, { property: "og:type", content: "website" }, { name: "twitter:card", content: "summary" }] }),
  component: Admin,
});

const TABS = [
  { k: "overview", l: "Dashboard", i: LayoutDashboard, C: AdminOverview },
  { k: "users", l: "Users", i: Users, C: AdminUsers },
  { k: "tasks", l: "Tasks", i: ListChecks, C: AdminTasks },
  { k: "subs", l: "Approvals", i: FileCheck2, C: AdminSubmissions },
  { k: "wd", l: "Withdrawals", i: Wallet, C: AdminWithdrawals },
  { k: "refs", l: "Referrals", i: Gift, C: AdminReferrals },
  { k: "settings", l: "Settings", i: Settings, C: AdminSettings },
  { k: "logs", l: "Activity Logs", i: ScrollText, C: AdminLogs },
];

function Admin() {
  const { data: me, isLoading } = useMe();
  const [tab, setTab] = useState("overview");
  if (isLoading) return <p className="p-8 text-center text-muted-foreground">Loading...</p>;
  if (!me?.isAdmin)
    return (
      <div className="grid min-h-screen place-items-center p-4"><div className="glass-card p-8 text-center"><ShieldAlert className="mx-auto h-10 w-10 text-destructive" /><p className="mt-3 font-bold">Admins only</p><Link to="/admin-login" className="mt-3 block text-primary">Admin login</Link></div></div>
    );
  const Active = TABS.find((t) => t.k === tab)!.C;
  return (
    <div className="min-h-screen md:flex">
      <aside className="border-b border-border bg-sidebar md:sticky md:top-0 md:h-screen md:w-60 md:border-b-0 md:border-r">
        <div className="flex items-center gap-2 p-4"><img src="/icons/icon-192.png" alt="" className="h-8 w-8 rounded-lg" /><span className="font-display font-bold">Admin Panel</span></div>
        <nav className="flex gap-1 overflow-x-auto px-2 pb-2 md:flex-col md:overflow-visible">
          {TABS.map((t) => (
            <button key={t.k} onClick={() => setTab(t.k)} className={`flex shrink-0 items-center gap-2 rounded-lg px-3 py-2 text-sm ${tab === t.k ? "bg-primary/15 font-semibold text-primary" : "text-muted-foreground hover:text-foreground"}`}><t.i className="h-4 w-4" />{t.l}</button>
          ))}
          <Link to="/dashboard" className="flex shrink-0 items-center gap-2 rounded-lg px-3 py-2 text-sm text-muted-foreground"><ArrowLeft className="h-4 w-4" /> User app</Link>
        </nav>
      </aside>
      <main className="flex-1 p-4 md:p-6"><Active /></main>
    </div>
  );
}
