"use client";

import { Field, type FieldProps } from "formik";
import Toggle from "@/components/ui/Toggle";
import { useTranslation } from "react-i18next";
import FilterFlagField from "./FilterFlagField";
import { useAuthStore } from "@/store/authStore";

interface OpportunityFilterFlagsProps {
  setFieldValue: (field: string, value: unknown) => void;
  /** The two lists show different subsets of the same form. */
  showVolunteerFields?: boolean;
  showLearnServeFields?: boolean;
}

/**
 * The tick-box half of the filter form: the volunteering classifications, the
 * development format pair, and "match my interests".
 *
 * Split from the field grids above it because the two halves are governed by
 * different things — the grids are shared by both lists, these are per-list —
 * and because "match my interests" additionally depends on the signed-in user.
 *
 * Each flag occupies its own row in the single-column filter form.
 */
export default function OpportunityFilterFlags({
  setFieldValue,
  showVolunteerFields,
  showLearnServeFields,
}: OpportunityFilterFlagsProps) {
  const { t } = useTranslation();
  const user = useAuthStore((s) => s.user);

  /* Exactly one group ever shows: `/opportunities` lists both kinds at once,
     where neither set of classifications applies to every card. */
  const volunteerOnly = Boolean(showVolunteerFields) && !showLearnServeFields;
  const learnServeOnly = Boolean(showLearnServeFields) && !showVolunteerFields;

  return (
    <>
      {volunteerOnly && (
        <div className="col-span-full mb-4 grid grid-cols-1 gap-3">
          <FilterFlagField
            name="isRelief"
            label={t("COMMON.RELIEF")}
            onToggle={(next) => setFieldValue("isRelief", next)}
          />
          <FilterFlagField
            name="isUrgent"
            label={t("COMMON.EMERGENCY")}
            onToggle={(next) => setFieldValue("isUrgent", next)}
          />
          <FilterFlagField
            name="isSpecialNeed"
            label={t("COMMON.SPECIAL.NEED")}
            onToggle={(next) => setFieldValue("isSpecialNeed", next)}
          />
        </div>
      )}
      {learnServeOnly && (
        <div className="col-span-full mb-4 grid grid-cols-1 gap-3">
          <FilterFlagField
            name="isInPerson"
            label={t("COMMON.IN_PERSON")}
            onToggle={(next) => setFieldValue("isInPerson", next)}
          />
          <FilterFlagField
            name="isOnline"
            label={t("COMMON.ONLINE")}
            onToggle={(next) => setFieldValue("isOnline", next)}
          />
        </div>
      )}

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