"use client";

import { useTranslation } from "react-i18next";
import Input from "@/components/ui/Input";
import SelectInput from "@/components/ui/SelectInput";
import AgeRange from "@/components/ui/AgeRange";
import {
  VOLUNTEER_CATEGORY_WITH_BENEFICIARIES,
  volunteerOpportunityNationalityOptions,
} from "@/data/Constants";
import { useLanguageStore } from "@/store/languageStore";
import type { VolunteerFormValues } from "./volunteerFormTypes";

interface VolunteerAudienceFieldsProps {
  values: VolunteerFormValues;
  setFieldValue: (field: string, value: unknown) => void;
  setFieldTouched: (field: string, touched: boolean) => void;
  genderOptions: Array<{ label: string; value: string }>;
  genderLoading: boolean;
}

/**
 * Columns 3–4 of the grid: who the opportunity is for — age, gender,
 * nationality — and, for a charity category, how many it serves.
 *
 * Rendered as a fragment so it stays inside the parent's four-column grid
 * rather than opening a second one.
 */
export default function VolunteerAudienceFields({
  values,
  setFieldValue,
  setFieldTouched,
  genderOptions,
  genderLoading,
}: VolunteerAudienceFieldsProps) {
  const { t } = useTranslation();
  const selectedLanguage = useLanguageStore((state) => state.language);

  return (
    <>
    {/* ---- Row 3: who it is for ---- */}
    <div className="min-w-0">
      <AgeRange name="age" label={t("COMMON.AGE")} />
      <p className="-mt-2 mb-4 text-xs text-secondary-102">
        {t("COMMON.MAXIMUM_AGE_OPTIONAL_HINT")}
      </p>
    </div>
    <SelectInput
      name="gender"
      label={t("COMMON.GENDER")}
      options={genderOptions}
      onChange={(selectedOption) =>
        setFieldValue("gender", selectedOption?.value || "")
      }
      disabled={genderLoading}
    />
    {/*
      BE-77 — was a «كويتيين فقط» tick among the flags at the
      bottom of the form. It is not a flag: it is one answer
      chosen from a list (three here since BE-109 dropped
      «غير ناطق بالعربية»), and it belongs with age and gender,
      which are the other two "who is this for" questions. An
      unticked box also read as "not answered yet" rather than
      "open to everyone", which the explicit «كل الجنسيات»
      fixes.
    */}
    <SelectInput
      name="opportunity_nationality"
      label={t("COMMON.OPPORTUNITY_NATIONALITY")}
      options={volunteerOpportunityNationalityOptions.map((option) => ({
        label:
          selectedLanguage === "ar"
            ? option.name_ar
            : option.name_en,
        value: option.value,
      }))}
      onChange={(selectedOption) =>
        setFieldValue(
          "opportunity_nationality",
          String(selectedOption?.value ?? "all")
        )
      }
    />
    <Input
      name="link"
      label={t("COMMON.WHATSAPP_LINK")}
      className="w-full"
      onFocus={() => setFieldTouched("link", true)}
    />
    {/*
      * Charity work is the only category the backend keeps a
      * beneficiaries count for — it nulls the column for every
      * other one — so the field appears with the category
      * rather than always, leaving the fourth column empty.
      */}
    {values.volunteerCategory ===
      VOLUNTEER_CATEGORY_WITH_BENEFICIARIES && (
      <Input
        name="beneficiariesCount"
        label={t("COMMON.BENEFICIARIES_COUNT")}
        type="text"
        onFocus={() =>
          setFieldTouched("beneficiariesCount", true)
        }
      />
    )}
    </>
  );
}
