import Link from "next/link";

type DocumentStatusItem = {
  key: string;
  label: string;
  complete: boolean;
  helpText: string;
};

type CompliancePrompt = {
  key: string;
  label: string;
  tone: "READY" | "ACTION" | "WATCH";
  body: string;
};

type HandoffItem = {
  key: string;
  label: string;
  tone: "READY" | "ACTION" | "WATCH";
  body: string;
};

export type DocumentStatusPanelProps = {
  entityLabel: string;
  editHref: string;
  checklist: DocumentStatusItem[];
  compliancePrompts: CompliancePrompt[];
  handoffItems: HandoffItem[];
};

function toneClasses(tone: CompliancePrompt["tone"]) {
  switch (tone) {
    case "READY":
      return "border-emerald-200 bg-emerald-50 text-emerald-700";
    case "WATCH":
      return "border-amber-200 bg-amber-50 text-amber-700";
    default:
      return "border-rose-200 bg-rose-50 text-rose-700";
  }
}

export function DocumentStatusPanel({ entityLabel, editHref, checklist, compliancePrompts, handoffItems }: DocumentStatusPanelProps) {
  const completeCount = checklist.filter((item) => item.complete).length;
  const missing = checklist.filter((item) => !item.complete);
  const readinessTone = missing.length === 0 ? "border-emerald-200 bg-emerald-50 text-emerald-700" : missing.length <= 2 ? "border-amber-200 bg-amber-50 text-amber-700" : "border-rose-200 bg-rose-50 text-rose-700";
  const readinessLabel = missing.length === 0 ? "Ready" : missing.length <= 2 ? "Needs paperwork follow-up" : "Missing important paperwork";

  return (
    <section className="rounded-2xl border bg-white p-6 shadow-sm">
      <div className="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
        <div>
          <h2 className="text-base font-semibold">Paperwork check</h2>
          <p className="mt-1 text-sm text-gray-500">Check whether this {entityLabel} has the paperwork needed for daily work.</p>
        </div>
        <div className={`rounded-2xl border px-4 py-3 text-sm ${readinessTone}`}>
          <div className="text-xs font-semibold uppercase tracking-wide">Record status</div>
          <div className="mt-1 font-semibold">{readinessLabel}</div>
          <div className="mt-1 text-xs">{completeCount} of {checklist.length} items complete</div>
        </div>
      </div>

      <div className="mt-5 grid gap-6 xl:grid-cols-[1.2fr_1fr]">
        <div className="space-y-4">
          <div className="rounded-2xl border bg-slate-50 p-4">
            <div className="mb-3 text-xs font-semibold uppercase tracking-wide text-slate-500">Paperwork and safety check</div>
            <div className="space-y-3">
              {checklist.map((item) => (
                <div key={item.key} className="flex items-start justify-between gap-4 rounded-xl border bg-white px-3 py-3 text-sm">
                  <div>
                    <div className="font-medium text-slate-900">{item.label}</div>
                    <div className="mt-1 text-xs text-slate-500">{item.helpText}</div>
                  </div>
                  <div className={`rounded-full border px-2 py-1 text-[10px] font-semibold uppercase tracking-wide ${item.complete ? "border-emerald-200 bg-emerald-50 text-emerald-700" : "border-rose-200 bg-rose-50 text-rose-700"}`}>{item.complete ? "Ready" : "Missing"}</div>
                </div>
              ))}
            </div>
          </div>

          <div className="rounded-2xl border bg-slate-50 p-4">
            <div className="mb-3 text-xs font-semibold uppercase tracking-wide text-slate-500">Missing paperwork prompts</div>
            <div className="space-y-3">
              {compliancePrompts.map((prompt) => (
                <div key={prompt.key} className={`rounded-xl border px-3 py-3 text-sm ${toneClasses(prompt.tone)}`}>
                  <div className="font-semibold">{prompt.label}</div>
                  <div className="mt-1 text-xs leading-5">{prompt.body}</div>
                </div>
              ))}
            </div>
          </div>
        </div>

        <div className="space-y-4">
          <div className="rounded-2xl border bg-slate-50 p-4">
            <div className="mb-3 text-xs font-semibold uppercase tracking-wide text-slate-500">Setup-to-dispatch details</div>
            <div className="space-y-3">
              {handoffItems.map((item) => (
                <div key={item.key} className={`rounded-xl border px-3 py-3 text-sm ${toneClasses(item.tone)}`}>
                  <div className="font-semibold">{item.label}</div>
                  <div className="mt-1 text-xs leading-5">{item.body}</div>
                </div>
              ))}
            </div>
          </div>

          <div className="rounded-2xl border bg-slate-50 p-4">
            <div className="mb-3 text-xs font-semibold uppercase tracking-wide text-slate-500">Missing paperwork</div>
            {missing.length ? (
              <div className="space-y-2">
                {missing.map((item) => (
                  <div key={item.key} className="rounded-xl border border-rose-200 bg-white px-3 py-3 text-sm text-slate-700">
                    <div className="font-medium text-slate-900">{item.label}</div>
                    <div className="mt-1 text-xs text-slate-500">{item.helpText}</div>
                  </div>
                ))}
              </div>
            ) : (
              <div className="rounded-xl border border-emerald-200 bg-white px-3 py-3 text-sm text-emerald-700">No paperwork issues are visible on this record.</div>
            )}
          </div>

          <Link href={editHref} className="inline-flex rounded-xl border bg-white px-4 py-2 text-sm font-medium text-slate-700 shadow-sm hover:bg-slate-50">
            Update paperwork and setup
          </Link>
        </div>
      </div>
    </section>
  );
}
