"use client";

import Link from "next/link";
import { Building2, IdCard, Mail, MapPin, Pencil } from "lucide-react";

import type { BusinessProfile } from "@/services/employee-login/employee-login.client";
import { FraAvatar, FraSection } from "./fra-primitives";

/**
 * Section 1 — who is travelling, read from the saved traveller profile.
 *
 * Read-only on purpose. The design draws an inline edit form here, but
 * POST /business/requests accepts no employee fields at all: name, department,
 * employee id and office location live on the traveller profile and change only
 * through POST /business/profile. An editable form in this overlay would
 * therefore either silently discard what was typed, or turn one submit into two
 * writes that can half-succeed — the request filed against a profile that did
 * not save. The pencil goes to the profile page instead, which is where that
 * edit actually belongs.
 *
 * The grade chip in the mockup ("L3 · Manager") has no backing field anywhere in
 * the API and is not rendered rather than invented.
 */
export function EmployeeSection({
  profile,
  email,
  isSelf = true,
  travellerName,
}: {
  profile: BusinessProfile | null;
  /** The traveller's work email. For a self-request this comes from the JWT —
   *  no endpoint returns the signed-in user's own work email on its own. */
  email: string;
  /** False when a colleague's trip is being arranged (PRD §6.1). */
  isSelf?: boolean;
  /** Resolved name, used when the profile has not loaded yet. */
  travellerName?: string;
}) {
  const name = [profile?.firstName, profile?.middleName, profile?.lastName]
    .filter(Boolean)
    .join(" ");
  const displayName = name || travellerName || email;

  const meta: Array<{ icon: typeof Mail; text: string; label: string }> = [];
  if (profile?.department)
    meta.push({ icon: Building2, text: profile.department, label: "Department" });
  meta.push({ icon: Mail, text: email, label: "Work email" });
  if (profile?.employeeId)
    meta.push({ icon: IdCard, text: profile.employeeId.toUpperCase(), label: "Employee ID" });
  if (profile?.officeLocation)
    meta.push({ icon: MapPin, text: profile.officeLocation, label: "Office location" });

  return (
    <FraSection
      step={1}
      // "Employee" is right when there is only one person on the request. The
      // moment a requester and a traveller can differ, the heading has to say
      // which one this block is.
      title={isSelf ? "Employee" : "Traveller"}
      action={
        isSelf ? (
          <Link
            href="/employee-profile"
            className="-m-2 inline-flex items-center gap-1.5 p-2 text-[13px] font-medium text-[var(--fra-muted)] transition-colors hover:text-[var(--fra-coral)] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--fra-coral)]"
          >
            <Pencil aria-hidden className="size-3.5" />
            Edit
            <span className="sr-only"> your profile</span>
          </Link>
        ) : (
          // No pencil on a colleague's card. Their profile is theirs to edit,
          // and POST /business/profile only ever writes the caller's own — a
          // link here would lead to the wrong person's form.
          <span className="rounded-full bg-[var(--fra-blue-bg)] px-[9px] py-[3px] text-[11px] font-bold text-[var(--fra-navy-2)]">
            Requested by you
          </span>
        )
      }
    >
      <div className="flex items-center gap-3.5">
        <FraAvatar name={displayName} className="size-12 text-base" />
        <div className="min-w-0">
          <strong className="block text-base font-bold text-[var(--fra-heading)]">
            {displayName}
          </strong>
          <ul className="mt-1 flex list-none flex-wrap gap-x-4 gap-y-1 p-0 text-[13px] text-[var(--fra-muted)]">
            {meta.map(({ icon: Icon, text, label }) => (
              <li key={label} className="inline-flex min-w-0 items-center gap-1.5">
                <Icon aria-hidden className="size-3.5 shrink-0" />
                <span className="sr-only">{label}: </span>
                <span className="truncate">{text}</span>
              </li>
            ))}
          </ul>
        </div>
      </div>
    </FraSection>
  );
}
