"use client";

import Link from "next/link";
import { useParams } from "next/navigation";
import { PageHeader } from "@/components/ui/page-header";
import {
  Card,
  ContinuityBadge,
  InfoRow,
  SmallStat,
  TruckSubnav,
  buildBrokerCallContinuity,
  buildContinuityActionQueue,
  buildNextMoveSummary,
  formatDate,
  formatMoney,
  getContinuityBuckets,
  useTruckCommandData,
} from "../truck-command-support";

export default function TruckNextLoadPage() {
  const params = useParams<{ id: string }>();
  const truckId = params?.id;
  const { truck, continuity, currentLoad, quickSnapshot, error } = useTruckCommandData(truckId);

  if (error) return <div className="rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-700">{error}</div>;
  if (!truck || !continuity || !truckId) return <div className="text-sm text-slate-500">Loading next load...</div>;

  const continuityBuckets = getContinuityBuckets(continuity.label);
  const summary = buildNextMoveSummary(truck, currentLoad, continuity);
  const actionQueue = buildContinuityActionQueue(truck, currentLoad, continuity);
  const brokerCall = buildBrokerCallContinuity(truck, currentLoad, continuity);

  return (
    <div className="space-y-5">
      <PageHeader
        title={`Truck ${truck.truckNumber} · Next Load`}
        subtitle="Use this page to protect the next move for this truck."
      />
      <TruckSubnav truckId={truckId} />

      <div className="grid gap-5 xl:grid-cols-[1.3fr_0.7fr]">
        <Card title="Next move" dense>
          <div className="grid gap-4 lg:grid-cols-[0.9fr_1.1fr]">
            <div className="rounded-xl border border-slate-200 p-4">
              <div className="flex items-start justify-between gap-3">
                <div>
                  <div className="text-[10px] font-semibold uppercase tracking-[0.18em] text-slate-500">Truck status</div>
                  <div className="mt-2 text-2xl font-semibold text-slate-950">{continuity.label}</div>
                </div>
                <ContinuityBadge label={continuity.label} tone={continuity.tone} />
              </div>
              <div className="mt-3 text-sm text-slate-700">{continuity.message}</div>
              <div className="mt-4 grid gap-2 sm:grid-cols-2">
                <SmallStat label="Protected" value={continuityBuckets.isProtected ? "Yes" : "No"} />
                <SmallStat label="At risk" value={continuityBuckets.isAtRisk ? "Yes" : "No"} />
                <SmallStat label="Needs next load today" value={continuityBuckets.isNeedsNextLoad ? "Yes" : "No"} />
                <SmallStat label="Uncovered" value={continuityBuckets.isUncovered ? "Yes" : "No"} />
              </div>
            </div>

            <div className="rounded-xl border border-slate-200 p-4">
              <div className="text-[10px] font-semibold uppercase tracking-[0.18em] text-slate-500">Next move summary</div>
              <div className="mt-2 text-xl font-semibold text-slate-950">{summary.headline}</div>
              <div className="mt-2 text-sm text-slate-700">{summary.body}</div>
              <div className="mt-4 grid gap-3 sm:grid-cols-2">
                <InfoRow label="Target lane" value={summary.lane} />
                <InfoRow label="Urgency" value={summary.urgency} />
                <InfoRow label="Broker" value={summary.brokerName} />
                <InfoRow label="Target rate" value={formatMoney(summary.rate)} />
                <InfoRow label="Current move delivery" value={formatDate(summary.deliveryWindow)} />
                <InfoRow label="Next pickup watch" value={formatDate(summary.pickupWindow)} />
              </div>
            </div>
          </div>
        </Card>

        <Card title="Next load check" dense>
          <div className="grid gap-2">
            <SmallStat label="Next move" value={continuity.label} />
            <SmallStat label="Current move ends" value={currentLoad?.deliveryLocation || "No active delivery"} />
            <SmallStat label="Follow-ups due" value={quickSnapshot.followUps} />
            <SmallStat label="Needs attention" value={quickSnapshot.openIssues} />
          </div>
          <div className="mt-4 flex flex-wrap gap-2">
            <Link href="/cori/quotes" className="inline-flex rounded-xl bg-black px-4 py-2 text-sm font-semibold text-white">{summary.ctaLabel}</Link>
            <Link href="/loads" className="inline-flex rounded-xl border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700">Open Loads</Link>
          </div>
        </Card>
      </div>

      <div className="grid gap-5 xl:grid-cols-[1.25fr_0.75fr]">
        <Card title="Next actions" dense>
          <div className="space-y-3">
            {actionQueue.map((action) => (
              <div key={action.title} className="flex flex-col gap-3 rounded-xl border border-slate-200 px-4 py-3 md:flex-row md:items-center md:justify-between">
                <div>
                  <div className="text-sm font-semibold text-slate-900">{action.title}</div>
                  <div className="mt-1 text-sm text-slate-600">{action.detail}</div>
                </div>
                <Link href={action.href} className="inline-flex rounded-xl border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700">
                  {action.cta}
                </Link>
              </div>
            ))}
          </div>
        </Card>

        <Card title="Latest broker call" dense>
          <div className="space-y-3 text-sm text-slate-700">
            <div className="rounded-xl border border-slate-200 px-3 py-3">
              <div className="text-[10px] font-semibold uppercase tracking-[0.18em] text-slate-500">Call result</div>
              <div className="mt-1 text-lg font-semibold text-slate-950">{brokerCall.outcome}</div>
              <div className="mt-2 text-sm text-slate-600">{brokerCall.note}</div>
            </div>
            <div className="rounded-xl border border-slate-200 px-3 py-3">
              <InfoRow label="Broker" value={brokerCall.brokerName} />
              <InfoRow label="Target rate" value={formatMoney(brokerCall.anchorRate)} />
              <InfoRow label="Next step" value={brokerCall.nextStep} />
              <InfoRow label="Callback" value={brokerCall.callback} />
            </div>
            <div className="grid gap-2 sm:grid-cols-2">
              <Link href={brokerCall.primaryAction.href} className="inline-flex items-center justify-center rounded-xl bg-black px-4 py-2 text-sm font-semibold text-white">
                {brokerCall.primaryAction.label}
              </Link>
              <Link href={brokerCall.secondaryAction.href} className="inline-flex items-center justify-center rounded-xl border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700">
                {brokerCall.secondaryAction.label}
              </Link>
            </div>
            <div className="rounded-xl border border-slate-200 px-3 py-3 text-sm text-slate-700">
              Work this truck first, not the whole company at once. The goal is to keep Truck {truck.truckNumber} moving without a gap.
            </div>
          </div>
        </Card>
      </div>
    </div>
  );
}
