"use client";

import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/Button";
import { Modal } from "@/components/ui/Modal";
import OpportuniteFilterModal from "./AllOpportuniteFilterModal";
import { type AllOpportunitiesFiltersData } from "./opportunityFilters";

interface OpportunityFilterDialogProps {
  open: boolean;
  onClose: () => void;
  filters: AllOpportunitiesFiltersData;
  onApply: (filters: AllOpportunitiesFiltersData) => void;
  onClear: () => void;
  /** Remounts the form, which is how a clear resets its fields. */
  clearFiltersKey: number;
  isFilterDirty: boolean;
  onDirtyChange: (dirty: boolean) => void;
  isFilterEmpty: boolean;
  onEmptyChange: (empty: boolean) => void;
  /** The two lists show different subsets of the same form. */
  showLearnServeFields: boolean;
  showVolunteerFields: boolean;
}

/**
 * The filter dialog for the opportunity lists.
 *
 * Was written out character-for-character in three list screens, differing only
 * in `showVolunteerFields`. Apply is disabled until something changes and Clear
 * until something is set, which is the pair most likely to drift between copies
 * — one list had already been left with a Clear button that was always enabled.
 *
 * Apply submits by finding the form in the DOM rather than through a ref: the
 * form is `OpportuniteFilterModal`'s own Formik, which does not expose one. Kept
 * as-is rather than changed while consolidating.
 */
export default function OpportunityFilterDialog({
  open,
  onClose,
  filters,
  onApply,
  onClear,
  clearFiltersKey,
  isFilterDirty,
  onDirtyChange,
  isFilterEmpty,
  onEmptyChange,
  showLearnServeFields,
  showVolunteerFields,
}: OpportunityFilterDialogProps) {
  const { t } = useTranslation();

  return (
    <Modal
      open={open}
      onClose={onClose}
      title={t("COMMON.FILTER")}
        compactFilter
      size="md"
      footer={
        <div className="flex xss:flex-col justify-center w-full gap-5">
          <Button
            variant="primary"
            size="medium"
            className="xss:!w-full"
            onClick={() => {
              const form = document.querySelector("form") as HTMLFormElement;
              if (form) form.requestSubmit();
            }}
            disabled={!isFilterDirty}
            type="button"
          >
            {t("COMMON.APPLY")}
          </Button>
          <Button
            variant="secondary"
            size="medium"
            className="xss:!w-full"
            onClick={onClear}
            disabled={isFilterEmpty}
            type="button"
          >
            {t("COMMON.CLEAR")}
          </Button>
        </div>
      }
    >
      <OpportuniteFilterModal
        key={clearFiltersKey}
        onApply={onApply}
        initialValues={filters}
        onDirtyChange={onDirtyChange}
        onEmptyChange={onEmptyChange}
        showLearnServeFields={showLearnServeFields}
        showVolunteerFields={showVolunteerFields}
      />
    </Modal>
  );
}
