"use client";

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

import type { BusinessProfile } from "@/services/employee-login/employee-login.client";
import {
  formatPassportDate,
  maskPassport,
  passportExpiry,
} from "@/services/employee-requests/passport";
import { HomeCard, HomeSection, HomeSkeleton } from "./HomeSection";

/**
 * "Passport on file" — E0 (LEIS-1018).
 *
 * ── Why the number is masked even though it is the reader's own ─────────
 *
 * It is on a landing page, which is the screen most likely to be open on a
 * shared desk or shown on a call. Nobody needs to read their own passport
 * number off a dashboard; they need to know the right one is on file, and the
 * last four answers that. The Profile page holds the full value for editing.
 *
 * ── Why six months ─────────────────────────────────────────────────────
 *
 * See `passportExpiry`. Short version: most destinations refuse a passport
 * with under six months left, so "still in date" is the wrong reassurance to
 * give somebody about to book international travel. The server agrees — it
 * raises `PASSPORT_EXPIRING_SOON` as a policy advisory on submit — but that
 * warning rides on the preview and submit responses, not on the requests list,
 * so this derives it from the profile rather than waiting for a request.
 */
export function PassportCard({
  profile,
  isLoading,
  /** Injected so this stays pure — see `passportExpiry`. */
  now,
}: {
  profile: BusinessProfile | null | undefined;
  isLoading: boolean;
  now?: Date;
}) {
  const expiry = passportExpiry(profile?.passportExpiry, now);
  const onFile = Boolean(profile?.passportNumber);

  return (
    <HomeSection title="Passport on file">
      <HomeCard>
        <div aria-live="polite" aria-busy={isLoading}>
          {isLoading ? (
            <>
              <span className="sr-only">Loading your passport details…</span>
              <HomeSkeleton rows={3} height={24} />
            </>
          ) : !onFile ? (
            <>
              <p className="m-0 text-[14px]" style={{ color: "var(--fra-text)" }}>
                You have no passport on file. Add one so we can book tickets in your passport
                name — international trips need it before you can request them.
              </p>
            </>
          ) : (
            <dl className="m-0 grid gap-[10px]">
              <Row label="Name as on passport" value={fullName(profile)} />
              <Row label="Nationality" value={profile?.nationality || "—"} />
              <Row label="Issuing country" value={profile?.passportIssuingCountry || "—"} />
              <Row label="Passport number" value={maskPassport(profile?.passportNumber)} />
              <Row
                label="Expires"
                value={expiry.state === "none" ? "—" : formatPassportDate(expiry.date)}
              />
            </dl>
          )}

          {(expiry.state === "expiring" || expiry.state === "expired") && (
            <p
              role="status"
              className="mt-[14px] mb-0 flex items-start gap-[8px] rounded-[10px] p-[12px] text-[13px]"
              style={{
                backgroundColor: "var(--fra-pending-bg)",
                border: "1px solid var(--fra-pending-border)",
                color: "var(--fra-pending)",
              }}
            >
              <AlertTriangle aria-hidden="true" className="mt-[1px] size-4 shrink-0" />
              <span>
                {expiry.state === "expired"
                  ? `Your passport expired on ${formatPassportDate(expiry.date)}. Update it before booking international travel.`
                  : `Your passport expires on ${formatPassportDate(expiry.date)}. Update it before booking international travel.`}
              </span>
            </p>
          )}
        </div>

        <Link
          href="/employee-profile"
          className="mt-[14px] inline-block text-[14px] font-semibold underline underline-offset-2"
          style={{ color: "var(--fra-navy-2)" }}
        >
          Edit profile
        </Link>
      </HomeCard>
    </HomeSection>
  );
}

function fullName(profile: BusinessProfile | null | undefined): string {
  const name = [profile?.firstName, profile?.middleName, profile?.lastName]
    .filter(Boolean)
    .join(" ");
  return name || "—";
}

function Row({ label, value }: { label: string; value: string }) {
  return (
    <div className="flex items-baseline justify-between gap-[16px]">
      <dt className="text-[14px]" style={{ color: "var(--fra-muted)" }}>
        {label}
      </dt>
      <dd
        className="m-0 text-right text-[14px] font-semibold break-all"
        style={{ color: "var(--fra-heading)" }}
      >
        {value}
      </dd>
    </div>
  );
}
