"use client";

import { useTranslation } from "react-i18next";
import SelectInput from "@/components/ui/SelectInput";
import { opportunityNationalityOptions } from "@/data/Constants";
import { useLanguageStore } from "@/store/languageStore";
import type { LearnServeFormValues } from "./learnServeFormTypes";

interface LearnServeCertificateFieldsProps {
  values: LearnServeFormValues;
  setFieldValue: (field: string, value: unknown) => void;
  certificateTypeOptions: Array<{ label: string; value: string }>;
  certificateTypeLoading: boolean;
  forsaCertificateId?: string;
}

/**
 * Certificate type and audience nationality.
 *
 * Both only apply to an internship or a course, so the caller renders this
 * conditionally rather than the fields hiding themselves — keeping the rule in
 * one place with the other type-dependent behaviour.
 */
export default function LearnServeCertificateFields({
  setFieldValue,
  certificateTypeOptions,
  certificateTypeLoading,
}: LearnServeCertificateFieldsProps) {
  const { t } = useTranslation();
  const selectedLanguage = useLanguageStore((state) => state.language);

  return (
    <>
      <div>
        <SelectInput
          name="certificateType"
          label={t("COMMON.CERTIFICATE")}
          options={certificateTypeOptions}
          onChange={(selectedOption) =>
            setFieldValue("certificateType", selectedOption?.value || "")
          }
          disabled={certificateTypeLoading}
        />
      </div>

      {/*
        Was a "Kuwaitis only" checkbox, then a two-way select. Now the client's
        four-way audience (BE-77): «الجميع» plus the three groups that
        partition everyone.
      */}
      <SelectInput
        name="opportunity_nationality"
        label={t("COMMON.OPPORTUNITY_NATIONALITY")}
        options={opportunityNationalityOptions.map((option) => ({
          label: selectedLanguage === "ar" ? option.name_ar : option.name_en,
          value: option.value,
        }))}
        onChange={(selectedOption) =>
          setFieldValue(
            "opportunity_nationality",
            String(selectedOption?.value ?? "all")
          )
        }
      />
    </>
  );
}
