"use client";

import { Field, useFormikContext, type FieldProps } from "formik";
import { useTranslation } from "react-i18next";
import AgeRange from "@/components/ui/AgeRange";
import BirthDateField from "@/components/ui/BirthDateField";
import SelectInput from "@/components/ui/SelectInput";
import Toggle from "@/components/ui/Toggle";
import { OpportunityStatus } from "@/data/Constants";
import { useLanguageStore } from "@/store/languageStore";
import { type AllEventsFiltersData } from "./AllEventFilterModal";
import { type ChoiceOption } from "./useAllEventFilters";

/**
 * The all-events filter form's fields.
 *
 * `isInnerModal` hides the date range: when this modal is opened from inside
 * another one, the dates are already fixed by the outer context.
 *
 * Location and the tag picker were removed on 2026-10-05 at the client's
 * request, matching the opportunity lists and the profile tabs. Both stay on
 * `AllEventsFiltersData` and in the cleared defaults — the list query still
 * forwards them — so with no control to set them they stay empty.
 */
export default function AllEventFilterFields({
  isInnerModal,
  genderOptions,
  eventTypeOptions,
  participationTypeOptions,
  genderLoading,
  eventTypeLoading,
  participationTypeLoading,
  isLoggedIn,
}: {
  isInnerModal?: boolean;
  genderOptions: ChoiceOption[];
  eventTypeOptions: ChoiceOption[];
  participationTypeOptions: ChoiceOption[];
  genderLoading: boolean;
  eventTypeLoading: boolean;
  participationTypeLoading: boolean;
  isLoggedIn: boolean;
}) {
  const { t } = useTranslation();
  const selectedLanguage = useLanguageStore((state) => state.language);
  /*
   * Read from context rather than taken as props: these fields render inside
   * the modal's `<Formik>`, so the bag is already available and prop-drilling
   * it would just be a second path to the same object.
   */
  const { setFieldValue } = useFormikContext<AllEventsFiltersData>();

  return (
          <div className="xss:pb-[20px] pb-[10px]">
            <div className="grid grid-cols-1 gap-0">
              {!isInnerModal && (
                <div className="col-span-1">
                  <SelectInput
                    name="type"
                    label={t("COMMON.TYPE")}
                    options={eventTypeOptions}
                    disabled={eventTypeLoading}
                    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 bordclr">
                <div className="flex flex-col gap-0">
                  <BirthDateField
                    name="startDate"
                    label={t("COMMON.START.DATE")}
                    rmdpClassname="placeholder-primary-5"
                  />
                  <BirthDateField
                    name="endDate"
                    label={t("COMMON.END.DATE")}
                    rmdpClassname="placeholder-primary-5"
                  />
                </div>
              </div>
            </div>

            <div className="grid grid-cols-1 gap-0">
              {/* Participation type takes the cell the location autocomplete
                  used to hold. It was rendered twice before — a `md:hidden`
                  copy in the row above and a `hidden md:block` full-width row
                  below — because it had no cell of its own at either width.
                  One cell, both widths, one copy. */}
              <div className="col-span-1">
                <SelectInput
                  name="participation_type"
                  label={t("COMMON.PARTICIPATION_TYPE")}
                  options={participationTypeOptions}
                  disabled={participationTypeLoading}
                  isModal={true}
                />
              </div>

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

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

            {isLoggedIn && (
              <div className="mb-6">
                <Field name="matchMyInterest">
                  {({ field }: FieldProps<boolean>) => (
                    <Toggle
                      label={t("COMMON.MATCH.INTREST")}
                      checked={field.value}
                      onChange={() => setFieldValue("matchMyInterest", !field.value)}
                    />
                  )}
                </Field>
              </div>
            )}
          </div>
  );
}
