import Link from "next/link";

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

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

export type OnboardingStatusPanelProps = {
  entityLabel: string;
  editHref: string;
  checklist: OnboardingChecklistItem[];
  contactPrompts: OnboardingPrompt[];
  onboardingPrompts: OnboardingPrompt[];
};

function toneClasses(tone: OnboardingPrompt["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 OnboardingStatusPanel({ entityLabel, editHref, checklist, contactPrompts, onboardingPrompts }: OnboardingStatusPanelProps) {
  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 follow-up" : "Missing important setup details";

  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">Setup check</h2>
          <p className="mt-1 text-sm text-gray-500">See whether this {entityLabel} record has the basic details 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">Status</div>
          <div className="mt-1 font-semibold">{readinessLabel}</div>
          <div className="mt-1 text-xs">{completeCount} of {checklist.length} core setup 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">Setup checklist</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 ? "Complete" : "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">Contact prompts</div>
            <div className="space-y-3">
              {contactPrompts.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">Missing setup details</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 visible onboarding gaps remain on this record.</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">Setup prompts</div>
            <div className="space-y-3">
              {onboardingPrompts.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>

          <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 setup details
          </Link>
        </div>
      </div>
    </section>
  );
}

export const OnboardingPaperworkPanel = OnboardingStatusPanel;
