"use client";

import { useTranslation } from "react-i18next";
import SelectInput from "@/components/ui/SelectInput";
import GroupedSelectInput from "@/components/ui/GroupedSelectInput";
import AgeRange from "@/components/ui/AgeRange";
import { nationalityFilterOptions, OpportunityStatus } from "@/data/Constants";
import { useLanguageStore } from "@/store/languageStore";
import {
  type TypeOption,
  type TypeOptionGroup,
} from "./opportunityTypeOptions";
import type { SelectInputOption } from "@/components/ui/SelectInput";
import type { ChoiceValue } from "@/lib/choices";
import OpportunityFilterFlags from "./OpportunityFilterFlags";

interface OpportunityFilterFieldsProps {
  setFieldValue: (field: string, value: unknown) => void;
  typeOptions: Array<TypeOption | TypeOptionGroup>;
  typeLoading: boolean;
  genderOptions: SelectInputOption<ChoiceValue>[];
  genderLoading: boolean;
  showVolunteerFields?: boolean;
  showLearnServeFields?: boolean;
}

/**
 * The filter form's field grids: type, status, gender, the age range and
 * nationality.
 *
 * The column spans are conditional on which list opened the form — the
 * volunteering-only list has no Type dropdown, so Status widens to fill the gap
 * rather than leaving a hole in the grid.
 *
 * Location, the start/end date pair and the tag picker were removed from all
 * three opportunity lists on 2026-10-05 at the client's request. They are still
 * on `AllOpportunitiesFiltersData`, deliberately: `ProfileFilterModal` offers
 * the same four on the profile tabs and reads the same type, and the list query
 * builder still forwards them. Here they simply stay at their cleared defaults,
 * so `opportunityListQueryParams` omits them.
 */
export default function OpportunityFilterFields({
  setFieldValue,
  typeOptions,
  typeLoading,
  genderOptions,
  genderLoading,
  showVolunteerFields,
  showLearnServeFields,
}: OpportunityFilterFieldsProps) {
  const { t } = useTranslation();
  const selectedLanguage = useLanguageStore((s) => s.language);

  /* The volunteering-only list is the one that has no Type dropdown: it is the
     only kind of opportunity the type filter could narrow to, so offering it
     there would be a control with one option. */
  const showType = !(showVolunteerFields && !showLearnServeFields);

  return (
    // Every filter occupies one full-width row.
    <div className="grid grid-cols-1 gap-0">
        {showType && (
          <div className="col-span-1">
            <GroupedSelectInput
              name="type"
              label={t("COMMON.TYPE")}
              options={typeOptions}
              disabled={typeLoading}
              isModal={true}
            />
          </div>
        )}
        <div className="col-span-1">
          <SelectInput
            name="status"
            label={t("COMMON.STATUS")}
            options={OpportunityStatus.map((g) => ({
              label: selectedLanguage === "ar" ? g.name_ar : g.name_en,
              value: g.value,
            }))}
            disabled={false}
            isModal={true}
          />
        </div>

        <div className="col-span-1">
          <SelectInput
            name="gender"
            label={t("COMMON.GENDER")}
            options={genderOptions}
            disabled={genderLoading}
            isModal={true}
          />
        </div>

        <div className="col-span-1">
          <div className="flex gap-2 bordclr">
            <AgeRange
              name="age"
              label={t("COMMON.AGE")}
              isModal={true}
              className="custom-border-age"
            />
          </div>
        </div>

        <div className="col-span-1">
          <SelectInput
            name="opportunity_nationality"
            label={t("COMMON.NATIONALITY_LABEL")}
            options={nationalityFilterOptions.map((g) => ({
              label: selectedLanguage === "ar" ? g.name_ar : g.name_en,
              value: g.value,
            }))}
            disabled={false}
            isModal={true}
          />
        </div>

        <div className="col-span-1">
          <SelectInput
            name="sortBy"
            label={t("COMMON.SORT_BY")}
            options={[
              { label: t("COMMON.SORT_NEWEST_FIRST"), value: "newest" },
              { label: t("COMMON.SORT_OLDEST_FIRST"), value: "oldest" },
            ]}
            disabled={false}
            isModal={true}
          />
        </div>
        <OpportunityFilterFlags
          setFieldValue={setFieldValue}
          showVolunteerFields={showVolunteerFields}
          showLearnServeFields={showLearnServeFields}
        />
    </div>
  );
}
