'use client';

import { Check } from 'lucide-react';

import type { VerifyOtpProfile } from '@/services/employee-signup/employee-signup.client';

interface DoneStepProps {
  profile: VerifyOtpProfile;
  onRequestTravelBooking: () => void;
}

function fullName(profile: VerifyOtpProfile): string {
  return [profile.firstName, profile.middleName, profile.lastName].filter(Boolean).join(' ');
}

function maskPassport(passportNumber: string | null): string {
  if (!passportNumber) return '—';
  if (passportNumber.length <= 4) return passportNumber;
  return `${'•'.repeat(passportNumber.length - 4)}${passportNumber.slice(-4)}`;
}

function formatDate(value: string | null): string {
  if (!value) return '—';
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return value;
  return date.toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' });
}

interface SummaryRowProps {
  label: string;
  value: string;
}

function SummaryRow({ label, value }: SummaryRowProps) {
  return (
    <li className="flex items-center justify-between gap-3 text-[13px]">
      <span className="text-[var(--fra-muted)]">{label}</span>
      <strong className="text-right font-semibold text-[var(--fra-heading)]">{value}</strong>
    </li>
  );
}

export default function DoneStep({ profile, onRequestTravelBooking }: DoneStepProps) {
  const name = fullName(profile) || 'there';

  return (
    <div className="mx-auto max-w-[440px] text-center">
      <span className="mx-auto mb-3.5 flex size-16 items-center justify-center rounded-full bg-[var(--fra-approved-bg)] text-[var(--fra-approved)]">
        <Check className="size-8" strokeWidth={3} />
      </span>

      <h2 className="mb-1.5 text-[22px] font-extrabold text-[var(--fra-heading)]">Account created</h2>
      <p className="mb-5 text-sm text-[var(--fra-muted)]">
        Welcome, <strong className="text-[var(--fra-heading)]">{name}</strong>. Your passport details are saved on
        your traveler profile and will fill in automatically when you request a flight.
      </p>

      <ul className="m-0 mb-5 grid list-none gap-2 rounded-xl border border-[var(--fra-border)] bg-[var(--fra-surface-2)] px-4 py-3.5 text-left">
        <SummaryRow label="Name as per passport" value={fullName(profile) || '—'} />
        <SummaryRow label="Employee ID" value={profile.employeeId ?? '—'} />
        <SummaryRow label="Date of birth" value={formatDate(profile.dateOfBirth)} />
        <SummaryRow label="Nationality" value={profile.nationality ?? '—'} />
        <SummaryRow
          label="Passport"
          value={`${maskPassport(profile.passportNumber)} · expires ${formatDate(profile.passportExpiry)}`}
        />
        <SummaryRow
          label="Mobile"
          value={profile.mobileNumber ? `${profile.mobileCountryCode ?? ''} ${profile.mobileNumber}` : '—'}
        />
      </ul>

      <button
        type="button"
        onClick={onRequestTravelBooking}
        className="group flex h-11 w-full items-center justify-center gap-2 rounded-[var(--fra-radius-sm)] bg-[var(--fra-coral)] text-[15px] font-semibold text-white shadow-[0_4px_14px_rgba(244,93,107,0.35)] transition-colors hover:bg-[var(--fra-coral-dark)]"
      >
        <span>Request Travel Booking</span>
        <svg
          className="size-4 transition-transform group-hover:translate-x-0.5"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          strokeWidth="2.5"
        >
          <path strokeLinecap="round" strokeLinejoin="round" d="M5 12h14M13 6l6 6-6 6" />
        </svg>
      </button>
    </div>
  );
}
