"use client";

import Link from "next/link";
import { useMemo } from "react";

type NavItem = {
  href: string;
  label: string;
  description: string;
};

type SummaryTone = "slate" | "blue" | "emerald" | "amber";

type SummaryItem = {
  label: string;
  value: number | string;
  helper: string;
  tone?: SummaryTone;
};

const NAV_ITEMS: NavItem[] = [
  { href: "/trucks", label: "Trucks", description: "Fleet units, equipment fit, and financial settings" },
  { href: "/drivers", label: "Drivers", description: "Driver readiness, follow-up, and truck linkage" },
  { href: "/owner-operators", label: "Owner Operators", description: "Capacity partners with linked driver and truck context" },
];

function toneClasses(tone: SummaryTone = "slate") {
  switch (tone) {
    case "blue":
      return "border-blue-200 bg-blue-50 text-blue-900";
    case "emerald":
      return "border-emerald-200 bg-emerald-50 text-emerald-900";
    case "amber":
      return "border-amber-200 bg-amber-50 text-amber-900";
    default:
      return "border-slate-200 bg-white text-slate-900";
  }
}

export function AssetWorkspaceNavigation({ currentHref }: { currentHref: string }) {
  return (
    <section className="rounded-3xl border border-slate-200 bg-white p-4 shadow-sm">
      <div className="flex flex-col gap-3 lg:flex-row lg:items-end lg:justify-between">
        <div>
          <div className="text-xs font-semibold uppercase tracking-[0.18em] text-slate-400">People & equipment workspace</div>
          <h2 className="mt-2 text-lg font-semibold text-slate-900">Fleet, workforce, and capacity stay connected</h2>
          <p className="mt-1 max-w-3xl text-sm text-slate-600">Use this path to move between trucks, drivers, and owner operators.</p>
        </div>
        <Link href="/dispatch" className="rounded-xl border border-slate-200 bg-slate-50 px-3 py-2 text-sm font-medium text-slate-700 hover:bg-slate-100">
          Open Dispatch Board
        </Link>
      </div>
      <div className="mt-4 grid gap-3 xl:grid-cols-3">
        {NAV_ITEMS.map((item) => {
          const active = currentHref === item.href;
          return (
            <Link
              key={item.href}
              href={item.href}
              className={`rounded-2xl border px-4 py-4 transition ${active ? "border-slate-900 bg-slate-900 text-white" : "border-slate-200 bg-slate-50 text-slate-900 hover:bg-white"}`}
            >
              <div className="text-sm font-semibold">{item.label}</div>
              <div className={`mt-1 text-xs ${active ? "text-slate-200" : "text-slate-500"}`}>{item.description}</div>
            </Link>
          );
        })}
      </div>
    </section>
  );
}

export function AssetWorkspaceLaunchCards({
  listHref,
  listLabel,
  createHref,
  createLabel,
}: {
  listHref: string;
  listLabel: string;
  createHref: string;
  createLabel: string;
}) {
  const cards = useMemo(
    () => [
      {
        href: listHref,
        eyebrow: "Current workspace",
        title: listLabel,
        detail: "Stay in the current list and keep scanning records with less page hopping.",
      },
      {
        href: createHref,
        eyebrow: "New record",
        title: createLabel,
        detail: "Create the next truck, driver, or partner record from the same workspace path.",
      },
      {
        href: "/dispatch",
        eyebrow: "Execution",
        title: "Dispatch board",
        detail: "Hand off into live work when a truck, driver, or owner operator is ready to move.",
      },
    ],
    [createHref, createLabel, listHref, listLabel],
  );

  return (
    <section className="grid gap-3 xl:grid-cols-3">
      {cards.map((card) => (
        <Link key={card.href + card.title} href={card.href} className="rounded-2xl border border-slate-200 bg-white p-4 shadow-sm transition hover:-translate-y-0.5 hover:shadow-md">
          <div className="text-[11px] font-semibold uppercase tracking-[0.18em] text-slate-400">{card.eyebrow}</div>
          <div className="mt-2 text-sm font-semibold text-slate-900">{card.title}</div>
          <p className="mt-1 text-sm text-slate-600">{card.detail}</p>
        </Link>
      ))}
    </section>
  );
}

export function AssetWorkspaceSummaryStrip({ items }: { items: SummaryItem[] }) {
  return (
    <section className="grid gap-3 sm:grid-cols-2 xl:grid-cols-4">
      {items.map((item) => (
        <div key={item.label} className={`rounded-2xl border p-4 shadow-sm ${toneClasses(item.tone)}`}>
          <div className="text-xs font-semibold uppercase tracking-wide text-slate-500">{item.label}</div>
          <div className="mt-2 text-2xl font-semibold">{item.value}</div>
          <div className="mt-1 text-sm text-slate-600">{item.helper}</div>
        </div>
      ))}
    </section>
  );
}

export function AssetDetailContinuity({
  currentHref,
  title,
  subtitle,
  continuityLabel,
  continuityTone = "slate",
  facts,
  sectionLinks,
  listHref,
  createHref,
  createLabel,
  relatedHref,
  relatedLabel,
}: {
  currentHref: string;
  title: string;
  subtitle: string;
  continuityLabel: string;
  continuityTone?: SummaryTone;
  facts: Array<{ label: string; value: React.ReactNode }>;
  sectionLinks: Array<{ href: string; label: string }>;
  listHref: string;
  createHref: string;
  createLabel: string;
  relatedHref?: string;
  relatedLabel?: string;
}) {
  return (
    <section className="rounded-3xl border border-slate-200 bg-white p-5 shadow-sm">
      <div className="grid gap-5 xl:grid-cols-[1.35fr_0.65fr]">
        <div>
          <div className="flex flex-wrap items-center gap-2">
            <span className="rounded-full border border-slate-200 bg-slate-50 px-3 py-1 text-xs font-semibold text-slate-600">People and equipment status</span>
            <span className={`rounded-full px-3 py-1 text-xs font-semibold ${toneClasses(continuityTone)}`}>{continuityLabel}</span>
          </div>
          <h2 className="mt-3 text-xl font-semibold text-slate-900">{title}</h2>
          <p className="mt-2 max-w-3xl text-sm text-slate-600">{subtitle}</p>
          <div className="mt-4 grid gap-3 sm:grid-cols-2 xl:grid-cols-4">
            {facts.map((fact) => (
              <div key={fact.label} className="rounded-2xl border border-slate-200 bg-slate-50 p-4">
                <div className="text-xs font-semibold uppercase tracking-wide text-slate-500">{fact.label}</div>
                <div className="mt-2 text-sm font-semibold text-slate-900">{fact.value}</div>
              </div>
            ))}
          </div>
          <div className="mt-4 flex flex-wrap gap-2">
            <Link href={listHref} className="rounded-xl border border-slate-200 bg-white px-3 py-2 text-sm font-medium text-slate-700">Back to list</Link>
            <Link href={createHref} className="rounded-xl bg-slate-900 px-3 py-2 text-sm font-medium text-white">{createLabel}</Link>
            <Link href="/dispatch" className="rounded-xl border border-slate-200 bg-white px-3 py-2 text-sm font-medium text-slate-700">Dispatch Board</Link>
            {relatedHref && relatedLabel ? <Link href={relatedHref} className="rounded-xl border border-slate-200 bg-white px-3 py-2 text-sm font-medium text-slate-700">{relatedLabel}</Link> : null}
          </div>
        </div>
        <div className="rounded-2xl border border-slate-200 bg-slate-50 p-4">
          <div className="text-xs font-semibold uppercase tracking-[0.18em] text-slate-400">Jump to section</div>
          <div className="mt-3 space-y-2">
            {sectionLinks.map((link) => (
              <a key={link.href} href={link.href} className="block rounded-xl border border-slate-200 bg-white px-3 py-3 text-sm font-medium text-slate-700 hover:bg-slate-100">
                {link.label}
              </a>
            ))}
          </div>
          <div className="mt-4 rounded-2xl border border-slate-200 bg-white p-4">
            <div className="text-xs font-semibold uppercase tracking-wide text-slate-500">Workspace path</div>
            <div className="mt-2 text-sm text-slate-700">Current area: {NAV_ITEMS.find((item) => item.href === currentHref)?.label ?? "Operations"}</div>
            <div className="mt-1 text-sm text-slate-500">Keep trucks, drivers, and owner operators close together when evaluating dispatch readiness.</div>
          </div>
        </div>
      </div>
    </section>
  );
}
