"use client";

import { useTranslation } from "react-i18next";
import Toggle from "@/components/ui/Toggle";
import { DO_NOT_DISTURB_FIELD_LIVE } from "@/data/Constants";
import { TRAINER_REQUEST_LIVE } from "@/features/trainer/trainerRequest";
import Input from "@/components/ui/Input";
import SelectInput from "@/components/ui/SelectInput";
import PhoneInput from "@/components/ui/PhoneInput";
import CountryCodeSelect from "./CountryCodeSelectLazy";
import type { FormikProps } from "formik";
import { type CombinedFormValues } from "./volunteerAccountTypes";
import type { ApiResponse } from "@/lib/api/types";
import type { AccountRecord } from "./volunteerAccountRecords";
import { type ChoiceSelectOption } from "./volunteerAccountDetailsProps";

interface GuardianSectionProps {
  values: CombinedFormValues;
  setFieldValue: FormikProps<CombinedFormValues>["setFieldValue"];
  relationshipOptions: ChoiceSelectOption[];
  relationshipLoading: boolean;
  /** Under 18 *today* — this form is the volunteer editing their own details. */
  under18: boolean;
  /** The saved account record, for the trainer-request state. */
  accountData: ApiResponse<AccountRecord> | undefined;
}

/**
 * The guardian block, shown only while the volunteer's date of birth puts them
 * under 18.
 *
 * The caller decides visibility (`under18`), so this renders unconditionally —
 * mounting it at all is the decision. Note the boundary differs from the
 * registration lists': there the age is measured **at registration**, because the
 * organizer needs whoever was the guardian then; here it is measured now,
 * because the volunteer is editing their own current details.
 */
export default function VolunteerGuardianSection(props: GuardianSectionProps) {
  const { t } = useTranslation();
  const {
    values,
    setFieldValue,
    relationshipOptions,
    relationshipLoading,
    under18,
    accountData,
  } = props;

  return (
    <>
                    {/* Emergency Contact Fields - Only shown if user is under 18 */}
                    {under18 && (
                      <>
                        <div className="mt-2 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 selectfiled">
                          <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>
                      </>
                    )}

                    {DO_NOT_DISTURB_FIELD_LIVE && (
                      <div className="w-[100%] pt-4">
                        <Toggle
                          label={t("COMMON.DO_NOT_DISTURB")}
                          checked={values.do_not_disturb}
                          onChange={() =>
                            setFieldValue("do_not_disturb", !values.do_not_disturb)
                          }
                        />
                        <p className="mt-1 text-sm text-secondary-102">
                          {t("COMMON.DO_NOT_DISTURB_HINT")}
                        </p>
                      </div>
                    )}

                    {TRAINER_REQUEST_LIVE && (
                      <div className="w-[100%] pt-4">
                        {/* An approved trainer's real name is public, so the
                            switch is locked on for them (BE-95 F). */}
                        <Toggle
                          label={t("COMMON.SHOW_REAL_NAME")}
                          checked={values.show_real_name}
                          disabled={accountData?.data?.is_trainer === true}
                          onChange={() =>
                            setFieldValue("show_real_name", !values.show_real_name)
                          }
                        />
                        <p className="mt-1 text-sm text-secondary-102">
                          {accountData?.data?.is_trainer === true
                            ? t("COMMON.SHOW_REAL_NAME_TRAINER_HINT")
                            : t("COMMON.SHOW_REAL_NAME_HINT")}
                        </p>
                      </div>
                    )}
    </>
  );
}
