"use client";

import Link from "next/link";
import { Check } from "lucide-react";
import { cn } from "cn";

/**
 * The three-step journey across the employee request flow: fill it in, send it,
 * track it. Shared by the Request-submitted screen and My requests so they read
 * as two stops on one path rather than two unrelated pages.
 *
 * It is a navigation aid, not a progress bar: steps already completed stay
 * clickable so someone can go back, and `<ol>` + `aria-current="step"` is what
 * tells assistive tech which one they are on. A step with nowhere to go is
 * rendered as plain text rather than a dead link.
 */

export interface StepperStep {
  label: string;
  caption: string;
  href?: string;
}

export function RequestStepper({
  steps,
  current,
  className,
}: {
  steps: StepperStep[];
  /** 1-based index of the step the user is on. */
  current: number;
  className?: string;
}) {
  return (
    <nav aria-label="Request progress" className={className}>
      <ol
        className={cn(
          "m-0 grid list-none gap-2 rounded-2xl border border-[var(--fra-border)] p-2",
          "bg-[var(--fra-surface)] shadow-[var(--fra-shadow)] sm:grid-cols-3"
        )}
      >
        {steps.map((step, i) => {
          const index = i + 1;
          const done = index < current;
          const isCurrent = index === current;

          const body = (
            <>
              <span
                aria-hidden
                className={cn(
                  "inline-flex size-8 shrink-0 items-center justify-center rounded-full border text-[13px] font-bold",
                  done
                    ? "border-transparent bg-[var(--fra-approved-solid)] text-white"
                    : isCurrent
                      ? "border-transparent bg-[var(--fra-navy-2)] text-white"
                      : "border-[var(--fra-border-strong)] bg-[var(--fra-surface-2)] text-[var(--fra-muted)]"
                )}
              >
                {done ? <Check className="size-4" /> : index}
              </span>
              <span className="min-w-0">
                <strong className="block truncate text-sm font-bold text-[var(--fra-heading)]">
                  {step.label}
                </strong>
                <small className="block truncate text-xs text-[var(--fra-muted)]">
                  {step.caption}
                </small>
              </span>
            </>
          );

          const shell = cn(
            "flex items-center gap-[12px] rounded-xl px-[12px] py-2.5 no-underline",
            isCurrent && "bg-[var(--fra-blue-bg)]"
          );

          return (
            <li key={step.label} className="relative min-w-0">
              {step.href && !isCurrent ? (
                <Link
                  href={step.href}
                  className={cn(
                    shell,
                    "transition-colors hover:bg-[var(--fra-hover)]",
                    "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--fra-coral)]"
                  )}
                >
                  {body}
                </Link>
              ) : (
                <span className={shell} aria-current={isCurrent ? "step" : undefined}>
                  {body}
                  {done && <span className="sr-only"> (completed)</span>}
                </span>
              )}
            </li>
          );
        })}
      </ol>
    </nav>
  );
}

/** The three steps, in one place so both screens label them identically. */
export function requestSteps(reviewHref: string): StepperStep[] {
  return [
    { label: "Request travel booking", caption: "Trip and business details", href: "/srp/flights" },
    { label: "Request submitted", caption: "Sent to your approver" },
    { label: "Request review", caption: "Track the decision", href: reviewHref },
  ];
}
