"use client";

import React from "react";
import OpportunityFormBoundary from "./OpportunityFormBoundary";
import VolunteerFormContent from "./VolunteerFormContent";
import { useVolunteerForm } from "./useVolunteerForm";
import { useTranslation } from "react-i18next";

import { Modal } from "@/components/ui/Modal";
import Loader from "@/components/ui/Loader";
import Title from "@/components/shared/Title";
import {
  VolunteerFormNavState,
  VolunteerFormProps,
} from "./volunteerFormTypes";

export type { VolunteerFormNavState };
import UpdateOpportunityConfirmModal from "./UpdateOpportunityConfirmModal";
import VolunteerRoleModal from "./VolunteerRoleModal";

/** Payload an opportunity card stashes before pushing to /volunteer-form. */
export default function VolunteerForm({
  autoSetTimeOnClick = true,
}: VolunteerFormProps) {
  const { t } = useTranslation();
  const form = useVolunteerForm();
  const {
    navState,
    isLoadingOpportunity,
    open,
    closeRoleModal,
    handleRoleModalClose,
    opportunityId,
    handleUpdateConfirm,
    showUpdateConfirmModal,
    setShowUpdateConfirmModal,
    showMismatchModal,
    setShowMismatchModal,
    setRoleModalRef,
    router,
  } = form;
  // `navState === null` means the stashed payload has not been read yet.
  if (navState === null) {
    return <Loader />;
  }

  return (
    <OpportunityFormBoundary id={form.id} isRepublish={form.isRepublish} source={form.opportunityData} loading={isLoadingOpportunity} failed={form.sourceFailed} error={form.sourceError} retry={() => { void form.refetchSource(); }}>
      <Modal
        open={open}
        onClose={handleRoleModalClose}
        title={t("COMMON.ROLE")}
        size="md"
      >
        <VolunteerRoleModal
          opportunityId={opportunityId ?? "0"}
          setMismatchChecker={(ref) => setRoleModalRef(ref)}
          showMismatchModal={showMismatchModal}
          setShowMismatchModal={setShowMismatchModal}
          onclose={() => {
            closeRoleModal();
            router.push("/posting-thankyou");
          }}
        />
      </Modal>

      <Modal
        open={showUpdateConfirmModal}
        onClose={() => setShowUpdateConfirmModal(false)}
        title={t("COMMON.UPDATE_OPPORTUNITY")}
        size="sm"
      >
        {/* The modal's copy is a fixed question, so it never read the
            `opportunityTitle` it used to be handed — the prop was declared and
            never destructured. Dropped rather than widened to the resource's
            real `string | null`. */}
        <UpdateOpportunityConfirmModal
          setOpenModal={() => setShowUpdateConfirmModal(false)}
          onConfirm={handleUpdateConfirm}
        />
      </Modal>

      <div className="border-t border-[#000]">
        <div className="compact-publish-form w-[90%] max-w-[1100px] mx-auto py-6 md:py-8">
          <h2>
            <Title text={t("COMMON.VOLUNTEER_FORM")} variant="default" />
          </h2>
          <VolunteerFormContent
            form={form}
            autoSetTimeOnClick={autoSetTimeOnClick}
          />
        </div>
      </div>
    </OpportunityFormBoundary>
  );
}
