"use client";

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

export default function TruckCurrentLoadPage() {
  const params = useParams<{ id: string }>();
  const truckId = params?.id;
  const { truck, currentLoad, 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 || !truckId) return <div className="text-sm text-slate-500">Loading current load...</div>;

  return (
    <div className="space-y-5">
      <PageHeader title={`Truck ${truck.truckNumber} · Current Load`} subtitle="Run the live move only: pickup, delivery, rate, status, and issue handling." />
      <TruckSubnav truckId={truckId} />
      <div className="grid gap-5 xl:grid-cols-[1.25fr_0.75fr]">
        <Card title="Live move" dense>
          {currentLoad ? (
            <>
              <InfoRow label="Status" value={currentLoad.status} />
              <InfoRow label="Broker" value={currentLoad.broker?.name} />
              <InfoRow label="Pickup" value={currentLoad.pickupLocation} />
              <InfoRow label="Delivery" value={currentLoad.deliveryLocation} />
              <InfoRow label="Pickup date" value={formatDate(currentLoad.pickupDate)} />
              <InfoRow label="Delivery date" value={formatDate(currentLoad.deliveryDate)} />
              <InfoRow label="Rate" value={formatMoney(currentLoad.invoiceAmount)} />
            </>
          ) : (
            <div className="text-sm text-slate-600">No active load is linked to this truck right now.</div>
          )}
        </Card>
        <Card title="Current load actions" dense>
          <div className="space-y-2 text-sm text-slate-700">
            <div className="rounded-xl border border-slate-200 px-3 py-2">Update pickup and delivery status from the active move.</div>
            <div className="rounded-xl border border-slate-200 px-3 py-2">Use Dispatch Board for live operational follow-up.</div>
            <div className="flex flex-wrap gap-2 pt-2">
              <Link href="/dispatch" className="inline-flex rounded-xl bg-black px-4 py-2 text-sm font-semibold text-white">Open dispatch board</Link>
              <Link href={`/trucks/${truck.id}/documents`} className="inline-flex rounded-xl border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700">Open paperwork</Link>
            </div>
          </div>
        </Card>
      </div>
    </div>
  );
}
