import type { PatientDetail } from "../../api/types";
import { checkInEligibility } from "../../logic/eligibility";
import type { PatientTab } from "../../logic/patients";
import { Button, Icon } from "../kit";
import { Link } from "../router";

const slugs: Record<PatientTab, string> = {
  overview: "",
  shared: "shared",
  check: "check",
  check_ins: "check-ins",
  records: "records",
  activity: "activity",
};
export const fromSlug = (slug: string | undefined): PatientTab =>
  (Object.entries(slugs).find(([, s]) => s === (slug ?? ""))?.[0] as PatientTab | undefined) ?? "overview";
export const tabPath = (id: string, tab: PatientTab) => `/app/patients/${id}${slugs[tab] ? `/${slugs[tab]}` : ""}`;

/** The button that starts a check-in, or why it cannot (YCI-1). Quiet by default: a page keeps one green action. */
export function AskYasmar({
  patient,
  findingId,
  checkId,
  label = "Ask Yasmar to check in",
  variant = "quiet",
}: {
  patient: PatientDetail;
  findingId?: string;
  checkId?: string | null;
  label?: string;
  variant?: "quiet" | "primary";
}) {
  const eligibility = checkInEligibility(patient);
  if (!eligibility.allowed)
    return (
      <span className="text-[12.5px] leading-[18px] text-muted" role="note">
        {eligibility.message}
      </span>
    );
  const q = new URLSearchParams();
  if (findingId) q.set("findingId", findingId);
  if (checkId) q.set("checkId", checkId);
  return (
    <Button variant={variant} asChild className="no-underline">
      <Link to={`/app/patients/${patient.id}/check-ins/new${q.size ? `?${q}` : ""}`}>
        <Icon name="phone-line" size={15} />
        {label}
      </Link>
    </Button>
  );
}
