"use client";

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

type Option = { label: string; value: string };

/** A choice localized at render time rather than by the API. */
type LocalizedOption = { value: string; name_en: string; name_ar: string };

interface VolunteerFieldGridProps {
  values: VolunteerFormValues;
  setFieldValue: (field: string, value: unknown) => void;
  setFieldTouched: (field: string, touched: boolean) => void;

  /** Whether the optional English title input is showing. */
  showEnglishTitle: boolean;
  onAddEnglishTitle: () => void;
  onRemoveEnglishTitle: () => void;
  /** The title spans the row when the category select is hidden. */
  titleColSpanClass: string;
  /** Not every publisher answers "what kind of opportunity is this?". */
  showOpportunitySection: boolean;

  volunteerCategoryOptions: readonly LocalizedOption[];
  opportunityPrivacyOptions: readonly LocalizedOption[];
  genderOptions: Option[];
  genderLoading: boolean;
  /** Clicking a time field opens the picker rather than typing into it. */
  autoSetTimeOnClick: boolean;
}

/**
 * The four-column field grid at the top of the volunteering form: what the
 * opportunity is, when it runs, and who it is for.
 *
 * Presentation only — every conditional it renders is decided by the caller.
 */
export default function VolunteerFieldGrid({
  values,
  setFieldValue,
  setFieldTouched,
  titleColSpanClass,
  showOpportunitySection,
  volunteerCategoryOptions,
  opportunityPrivacyOptions,
  genderOptions,
  genderLoading,
  autoSetTimeOnClick,
}: VolunteerFieldGridProps) {
  const { t } = useTranslation();
  const selectedLanguage = useLanguageStore((state) => state.language);

  return (
    <div className="grid grid-cols-1 gap-x-6 md:grid-cols-2 lg:grid-cols-4">
      {/* ---- Row 1: what it is ---- */}
      <div className={titleColSpanClass}>
        <Input
          name="title_ar"
          label={t("COMMON.ENTER_TITLE")}
          type="text"
          maxLength={255}
          dir="auto"
          onFocus={() => setFieldTouched("title_ar", true)}
        />
      </div>

      {showOpportunitySection && (
        <SelectInput
          name="isPrivate"
          label={t("COMMON.OPPORTUNITY.SHOULD.BE")}
          options={opportunityPrivacyOptions.map((option) => ({
            value: option.value,
            label:
              selectedLanguage === "ar"
                ? option.name_ar
                : option.name_en,
          }))}
          placeholder={t("COMMON.SELECT")}
        />
      )}

      <SelectInput
        name="volunteerCategory"
        label={t("COMMON.VOLUNTEER_CATEGORY")}
        placeholder={t("COMMON.SELECT")}
        options={volunteerCategoryOptions.map((option) => ({
          value: option.value,
          label:
            selectedLanguage === "ar"
              ? option.name_ar
              : option.name_en,
        }))}
        onChange={(selectedOption) => {
          const next = selectedOption?.value || "";
          setFieldValue("volunteerCategory", next);
          // Leaving charity drops the count the backend would
          // null anyway, so the hidden field can't go stale.
          if (next !== VOLUNTEER_CATEGORY_WITH_BENEFICIARIES) {
            setFieldValue("beneficiariesCount", "");
          }
        }}
      />

      <VolunteerScheduleFields
        setFieldTouched={setFieldTouched}
        autoSetTimeOnClick={autoSetTimeOnClick}
      />
      <VolunteerAudienceFields
        values={values}
        setFieldValue={setFieldValue}
        setFieldTouched={setFieldTouched}
        genderOptions={genderOptions}
        genderLoading={genderLoading}
      />
    </div>
  );
}
