import type { ReactNode } from "react";
import Link from "next/link";
import { CrmWorkspaceNavigation } from "@/components/crm/crm-workspace-navigation";

type ContinuityTone = "good" | "watch" | "action";

function toneClasses(tone: ContinuityTone) {
  if (tone === "good") return "bg-emerald-100 text-emerald-700";
  if (tone === "watch") return "bg-amber-100 text-amber-800";
  return "bg-rose-100 text-rose-700";
}

export function CrmDetailWorkspaceContinuity({
  currentHref,
  listHref,
  listLabel,
  createHref,
  createLabel,
  title,
  subtitle,
  continuityLabel,
  continuityTone = "good",
  facts,
  sectionLinks,
  analyticsHref = "/crm",
  intelligenceHref = "/cori?source=crm&question=Help me choose the best CRM follow-up.",
}: {
  currentHref: string;
  listHref: string;
  listLabel: string;
  createHref: string;
  createLabel: string;
  title: string;
  subtitle: string;
  continuityLabel: string;
  continuityTone?: ContinuityTone;
  facts: Array<{ label: string; value: ReactNode }>;
  sectionLinks: Array<{ href: string; label: string }>;
  analyticsHref?: string;
  intelligenceHref?: string;
}) {
  return (
    <div className="space-y-4">
      <CrmWorkspaceNavigation currentHref={currentHref} />

      <section className="grid gap-4 xl:grid-cols-[1.25fr_0.95fr]">
        <div className="rounded-2xl border bg-white p-5 shadow-sm">
          <div className="flex flex-col gap-3 lg:flex-row lg:items-start lg:justify-between">
            <div>
              <div className="text-[11px] font-semibold uppercase tracking-[0.18em] text-slate-400">Partner profile</div>
              <div className="mt-1 text-lg font-semibold text-slate-950">{title}</div>
              <div className="mt-1 text-sm text-slate-500">{subtitle}</div>
            </div>
            <div className={`rounded-full px-3 py-1.5 text-xs font-semibold ${toneClasses(continuityTone)}`}>{continuityLabel}</div>
          </div>

          <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 bg-slate-50 p-4">
                <div className="text-[11px] font-semibold uppercase tracking-[0.16em] text-slate-400">{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 bg-white px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Back to {listLabel}</Link>
            <Link href={analyticsHref} className="rounded-xl border bg-white px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Open CRM</Link>
            <Link href={intelligenceHref} className="rounded-xl border bg-white px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Ask CORI</Link>
            <Link href={createHref} className="rounded-xl bg-slate-900 px-4 py-2 text-sm font-medium text-white hover:bg-slate-800">{createLabel}</Link>
          </div>
        </div>

        <div className="rounded-2xl border bg-white p-5 shadow-sm">
          <div className="text-[11px] font-semibold uppercase tracking-[0.18em] text-slate-400">Jump to what matters</div>
          <div className="mt-1 text-lg font-semibold text-slate-950">Find the right section fast</div>
          <div className="mt-1 text-sm text-slate-500">Each relationship page starts with the summary. Paperwork, notes, and follow-up controls stay lower on the page.</div>
          <div className="mt-4 flex flex-wrap gap-2">
            {sectionLinks.map((section) => (
              <Link key={section.href} href={section.href} className="rounded-full border bg-white px-3 py-1.5 text-xs font-medium text-slate-700 hover:bg-slate-50">
                {section.label}
              </Link>
            ))}
          </div>
        </div>
      </section>
    </div>
  );
}
