import Link from "next/link";
import TimeTrackingAdminPanel from "@/components/admin/TimeTrackingAdminPanel";
import { FleetRouteSummary } from "@/components/time-tracking/admin/DailyRouteSummary";
import { getAdminUser } from "@/lib/admin-permissions";
import { redirect } from "next/navigation";
import { PageHeader } from "@/components/admin/design-system";
import { getFleetRouteSummary } from "@/actions/time-tracking/route-summary";

const TUXEDO_BRAND_ID = "64294306-5f42-463d-a5e8-2ad6c81a96de";
const IRD_BRAND_ID = "b1cb7a96-d82b-40b1-80b1-d6dd26c56e28";

export const dynamic = "force-dynamic";

const TABS = [
  { href: "/admin/time-tracking", label: "Overview" },
  { href: "/admin/time-tracking/timesheets", label: "Timesheets" },
  { href: "/admin/time-tracking/approvals", label: "Approvals" },
  { href: "/admin/time-tracking/audit", label: "Audit" },
];

export default async function AdminTimeTrackingPage({
  searchParams,
}: {
  searchParams: Promise<{ tab?: string }>;
}) {
  const adminUser = await getAdminUser();
  if (!adminUser) redirect("/login");

  const isPlatformAdmin = adminUser.role === "platform_admin";
  const isTuxedoAdmin =
    adminUser.role === "brand_admin" && adminUser.brand_id === TUXEDO_BRAND_ID;
  const isIRDAdmin =
    adminUser.role === "brand_admin" && adminUser.brand_id === IRD_BRAND_ID;

  if (!isPlatformAdmin && !isTuxedoAdmin && !isIRDAdmin) {
    redirect("/admin/pickup");
  }

  const effectiveBrandId = adminUser.brand_id ?? TUXEDO_BRAND_ID;
  const sp = await searchParams;
  const activeTab = sp.tab ?? "overview";

  return (
    <div className="min-h-screen bg-[var(--admin-bg)]">
      <PageHeader
        title="Time Tracking"
        subtitle="Manage workers, tasks, timesheets, and approvals"
        className="px-6 pt-6"
      />
      <div className="px-6 pb-8">
        {/* Tab nav */}
        <nav className="mb-6 flex gap-1 rounded-xl border border-zinc-200 bg-white p-1">
          {TABS.map((t) => {
            const active =
              t.href === "/admin/time-tracking"
                ? activeTab === "overview" || t.href === `/admin/time-tracking${activeTab === "overview" ? "" : ""}`
                : activeTab && t.href.endsWith(activeTab);
            return (
              <Link
                key={t.href}
                href={t.href}
                className={`flex-1 rounded-lg px-4 py-2 text-center text-sm font-medium transition-colors ${
                  active
                    ? "bg-zinc-900 text-white"
                    : "text-zinc-600 hover:bg-zinc-100"
                }`}
              >
                {t.label}
              </Link>
            );
          })}
        </nav>

        <TimeTrackingAdminPanel brandId={effectiveBrandId} />

        <div className="mt-6">
          <FleetRouteSummaryClient brandId={effectiveBrandId} />
        </div>
      </div>
    </div>
  );
}

async function FleetRouteSummaryClient({ brandId }: { brandId: string }) {
  const today = new Date().toISOString().slice(0, 10);
  const rows = await getFleetRouteSummary(brandId, today);
  return <FleetRouteSummary brandId={brandId} rows={rows} date={today} />;
}