"use client";

import Link from "next/link";
import { useMemo } from "react";
import { useSearchParams } from "next/navigation";
import { PageHeader } from "@/components/ui/page-header";
import { CallCopilotShell } from "@/components/cori/call-copilot-shell";
import type { CallCopilotLaunchContext } from "@/components/cori/call-copilot-support";

export default function CoriQuotesPage() {
  const searchParams = useSearchParams();

  const launchContext = useMemo<CallCopilotLaunchContext>(() => ({
    contactId: searchParams.get("contactId") ?? undefined,
    contactType: searchParams.get("contactType") ?? undefined,
    contactName: searchParams.get("contactName") ?? undefined,
    phone: searchParams.get("phone") ?? undefined,
    companyLabel: searchParams.get("companyLabel") ?? undefined,
    truckLabel: searchParams.get("truck") ?? searchParams.get("truckLabel") ?? undefined,
    laneLabel: searchParams.get("lane") ?? searchParams.get("laneLabel") ?? undefined,
    targetRate: searchParams.get("targetRate") ?? undefined,
    floorRate: searchParams.get("floorRate") ?? undefined,
    purpose: searchParams.get("purpose") ?? undefined,
  }), [searchParams]);

  return (
    <div className="space-y-6">
      <PageHeader
        title="Call Copilot"
        subtitle="Call the contact, keep key facts, and let CORI suggest the next line when you need help."
        action={
          <div className="flex flex-wrap gap-2">
            <Link href="/cori" className="inline-flex rounded-xl border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Back to CORI</Link>
          </div>
        }
      />

      <CallCopilotShell variant="page" launchContext={launchContext} />
    </div>
  );
}
