"use client";

import { Check, Hourglass, MessageSquare, X } from "lucide-react";
import { cn } from "cn";

import type {
  TrackerStep,
  TrackerState,
} from "@/services/employee-requests/employee-requests.types";
import { formatDateTime } from "@/services/employee-requests/format";

/**
 * The four-step tracker on a My-requests card.
 *
 * **Rendered from the server's `tracker`, never recomputed.** The labels are
 * business logic rather than formatting, and the contract spells out four ways
 * deriving them locally goes wrong: a rejected request reads "Not required"
 * under Booking and Ticket, a replied-to one reads "Reply sent, in review", the
 * fourth step is "Ticket issued" for a flight but "Confirmation issued" for a
 * hotel, and it only completes once the booking *document* exists — not when
 * the status becomes BOOKED. This file maps `state` onto a colour and an icon
 * and does nothing else.
 *
 * The connector is drawn with a pseudo-element on each step rather than a
 * single bar behind them, so it reflows with the grid at any width instead of
 * needing its length recalculated.
 */

const DOT: Record<TrackerState, string> = {
  done: "border-transparent bg-[var(--fra-approved-solid)] text-white",
  current:
    "border-transparent bg-[var(--fra-info-solid)] text-white shadow-[0_0_0_4px_var(--fra-info-bg)]",
  info: "border-transparent bg-[var(--fra-info-solid)] text-white shadow-[0_0_0_4px_var(--fra-info-bg)]",
  blocked: "border-transparent bg-[var(--fra-rejected-solid)] text-white",
  skipped: "border-[var(--fra-border-strong)] bg-[var(--fra-surface-2)] text-[var(--fra-muted)]",
  upcoming: "border-[var(--fra-border-strong)] bg-[var(--fra-surface-2)] text-[var(--fra-muted)]",
};

const BAR: Record<TrackerState, string> = {
  done: "bg-[var(--fra-approved-solid)]",
  current: "bg-[linear-gradient(90deg,var(--fra-info-solid),var(--fra-border))]",
  info: "bg-[linear-gradient(90deg,var(--fra-info-solid),var(--fra-border))]",
  blocked: "bg-[var(--fra-border)]",
  skipped: "bg-[var(--fra-border)]",
  upcoming: "bg-[var(--fra-border)]",
};

const ICON: Partial<Record<TrackerState, typeof Check>> = {
  done: Check,
  current: Hourglass,
  info: MessageSquare,
  blocked: X,
};

/** Spoken equivalent of the colour, so the state is not carried by hue alone. */
const SPOKEN: Record<TrackerState, string> = {
  done: "completed",
  current: "in progress",
  info: "waiting on you",
  blocked: "stopped",
  skipped: "not required",
  upcoming: "not started",
};

export function RequestTracker({ steps }: { steps: TrackerStep[] }) {
  return (
    <ol
      className="m-0 mb-[16px] grid list-none grid-cols-2 gap-y-4 p-0 sm:gap-y-0 md:grid-cols-4"
      // The whole track is one status readout; announcing four separate list
      // items as they change would be noise.
      aria-label="Request progress"
    >
      {steps.map((step, i) => {
        const Icon = ICON[step.state];
        const last = i === steps.length - 1;
        return (
          <li
            key={step.key}
            className={cn(
              "relative min-w-0 pr-[12px]",
              (step.state === "skipped" || step.state === "upcoming") && "opacity-60"
            )}
          >
            {!last && (
              <span
                aria-hidden
                className={cn(
                  "absolute top-[11px] right-0 left-6 hidden h-[3px] rounded md:block",
                  BAR[step.state]
                )}
              />
            )}
            <span
              aria-hidden
              className={cn(
                "mb-1.5 inline-flex size-6 items-center justify-center rounded-full border",
                DOT[step.state]
              )}
            >
              {Icon && <Icon className="size-3.5" strokeWidth={3} />}
            </span>
            <strong
              className={cn(
                "block text-[13px] font-semibold",
                step.state === "blocked" ? "text-[var(--fra-rejected)]" : "text-[var(--fra-text)]"
              )}
            >
              {step.label}
              <span className="sr-only"> — {SPOKEN[step.state]}</span>
            </strong>
            <small
              className={cn(
                "mt-0.5 block text-xs",
                step.state === "current" || step.state === "info"
                  ? "font-medium text-[var(--fra-info)]"
                  : "text-[var(--fra-muted)]"
              )}
            >
              {step.at ? formatDateTime(step.at) : (step.note ?? "—")}
            </small>
          </li>
        );
      })}
    </ol>
  );
}
