"use client";

import type { FieldErrors, UseFormRegister } from "react-hook-form";

import type { MeetingTypeOption } from "@/services/employee-requests/employee-requests.types";
import type { HotelRequestApprovalValues } from "@/features/employee-requests/schemas/hotel-request-approval.schema";
import {
  FraField,
  FraInput,
  FraSection,
  FraSelect,
  FraTextarea,
} from "@/components/employee-requests/fra-primitives";

/**
 * LEIS-990 — the hotel request's Business information: Client (optional),
 * Meeting type (required), Business justification (required). Not a reuse
 * of flights' BusinessInfoSection — that component always renders
 * Opportunity, Priority and Additional cost too, none of which LEIS-990
 * calls for on a hotel stay, and it has no prop to hide them.
 */
export function HotelBusinessInfoSection({
  step,
  register,
  errors,
  meetingTypes,
  meetingTypesLoading,
  meetingTypesFailed,
  onRetryMeetingTypes,
}: {
  step?: number;
  register: UseFormRegister<HotelRequestApprovalValues>;
  errors: FieldErrors<HotelRequestApprovalValues>;
  meetingTypes: MeetingTypeOption[];
  meetingTypesLoading: boolean;
  meetingTypesFailed: boolean;
  onRetryMeetingTypes: () => void;
}) {
  return (
    <FraSection step={step} title="Business information">
      <div className="grid grid-cols-1 gap-x-4 gap-y-3.5 sm:grid-cols-2">
        <FraField id="client" label="Client" error={errors.client?.message} className="sm:col-span-2">
          {(p) => <FraInput {...p} {...register("client")} placeholder="e.g. ABC Corporation" />}
        </FraField>

        <FraField
          id="meetingType"
          label="Meeting type"
          required
          error={
            errors.meetingType?.message ??
            (meetingTypesFailed ? "We could not load the meeting types." : undefined)
          }
          className="sm:col-span-2"
        >
          {(p) =>
            meetingTypesFailed ? (
              <button
                type="button"
                onClick={onRetryMeetingTypes}
                className="min-h-[38px] w-full rounded-[var(--fra-radius-sm)] border border-[var(--fra-border-strong)] px-[12px] py-2 text-left text-sm font-medium text-[var(--fra-navy-2)] hover:bg-[var(--fra-hover)]"
              >
                Try loading them again
              </button>
            ) : (
              <FraSelect {...p} {...register("meetingType")} disabled={meetingTypesLoading}>
                <option value="">{meetingTypesLoading ? "Loading…" : "Select meeting type"}</option>
                {meetingTypes.map((t) => (
                  <option key={t.label} value={t.label}>
                    {t.label}
                  </option>
                ))}
              </FraSelect>
            )
          }
        </FraField>

        <FraField
          id="justification"
          label="Business justification"
          required
          error={errors.justification?.message}
          className="sm:col-span-2"
        >
          {(p) => <FraTextarea {...p} {...register("justification")} rows={3} placeholder="Why this stay?" />}
        </FraField>
      </div>
    </FraSection>
  );
}
