"use client";

import React from "react";
import Input from "@/components/ui/Input";
import PhoneInput from "@/components/ui/PhoneInput";
import { useTranslation } from "react-i18next";
import CountryCodeSelect from "./CountryCodeSelectLazy";
import SelectInput from "@/components/ui/SelectInput";
import type { FormikProps } from "formik";
import type { IndividualRegistrationValues } from "./individualRegistrationValues";
import type { SelectInputOption } from "@/components/ui/SelectInput";

/** A field group lifted out of the form of the same family. */
export default function IndividualEmergencyFields({
  values,
  setFieldValue,
  relationshipOptions,
  relationshipLoading,
}: {
  values: IndividualRegistrationValues;
  setFieldValue: FormikProps<IndividualRegistrationValues>["setFieldValue"];
  /* Numeric: these come straight from `/choices/`, whose ids are integers. */
  relationshipOptions: SelectInputOption<number>[];
  relationshipLoading: boolean;
}) {
  const { t } = useTranslation();

  return (
  <>
    <div className="mt-6 mb-4">
      <h3 className="text-lg font-semibold text-primary-5">
        {t("COMMON.EMERGENCY_CONTACT_DETAILS")}
      </h3>
      <p className="text-sm text-gray-600 mt-1">
        {t("COMMON.EMERGENCY_CONTACT_REQUIRED_UNDER_18")}
      </p>
    </div>

    <div className="grid grid-cols-2 mobilescreen:grid-cols-1 mobilescreen:gap-0 gap-6">
      <Input
        name="emergency_contact_name"
        type="text"
        label={t("COMMON.EMERGENCY_CONTACT_NAME")}
        lettersOnly
      />

      <SelectInput
        name="emergency_contact_relationship"
        label={t("COMMON.EMERGENCY_CONTACT_RELATIONSHIP")}
        options={relationshipOptions}
        onChange={(selectedOption) =>
          setFieldValue("emergency_contact_relationship", selectedOption?.value || "")
        }
        disabled={relationshipLoading}
      />
    </div>

    <div className="grid grid-cols-2 mobilescreen:grid-cols-1 mobilescreen:gap-0 gap-6">
      <div className="flex gap-2">
        <div>
          <CountryCodeSelect
            name="emergency_contact_country_code"
            className="w-full"
            onChange={(value: string) =>
              setFieldValue("emergency_contact_country_code", value)
            }
          />
        </div>
        <div className="w-full">
          <PhoneInput
            name="emergency_contact_phone"
            label={t("COMMON.EMERGENCY_CONTACT_PHONE")}
            className="w-full"
            countryCode={values.emergency_contact_country_code}
          />
        </div>
      </div>

      <Input
        name="emergency_contact_civil_id"
        type="text"
        label={t("COMMON.EMERGENCY_CONTACT_CIVIL_ID")}
        maxLength={12}
        digitsOnly
      />
    </div>
  </>
  );
}
