"use client";

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

import Loader from "@/components/ui/Loader";
import Title from "@/components/shared/Title";
import { useLearnServeForm } from "./useLearnServeForm";
import LearnServeFormContent from "./LearnServeFormContent";
import LearnServeFormModals from "./LearnServeFormModals";
import {
  LearnServeFormNavState,
  LearnServeFormProps,
} from "./learnServeFormTypes";

export type { LearnServeFormNavState };

/**
 * A rich-text editor never reports an empty string once it has been focused —
 * it leaves `<p></p>` or `<p><br></p>` behind — so "is there a description?"
 * has to be asked of the text, not of the markup. Same helper as
 * `VolunteerForm`, which asks the same question.
 */
/** Payload a learn & serve card stashes before pushing to /learn-and-share-form. */

export default function LearnServeForm({
  autoSetTimeOnClick = true,
  // Renamed: `opportunityId` is already taken by the in-progress-create state below.
  opportunityId: editOpportunityId,
}: LearnServeFormProps) {
  const { t } = useTranslation();
  const form = useLearnServeForm(editOpportunityId);
  const {
    navState,
    isLoadingOpportunity,
    opportunityData,
    opportunityId,
    opportunityDetails,
    refetchTimeSlots,
    opentime,
    handleCloseTimeModal,
    confirmModalOpen,
    handleConfirmDeleteTimeSlots,
    handleCancelDeleteTimeSlots,
    showUpdateConfirmModal,
    pendingFormData,
    setPendingFormData,
    setShowUpdateConfirmModal,
    handleUpdateConfirm,
  } = 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={opportunityData} loading={isLoadingOpportunity} failed={form.sourceFailed} error={form.sourceError} retry={() => { void form.refetchSource(); }}>
      <LearnServeFormModals
        opentime={opentime}
        opportunityId={opportunityId}
        opportunityDetails={opportunityDetails}
        onCloseTimeModal={handleCloseTimeModal}
        onTimingsChanged={() => {
          if (opportunityId) refetchTimeSlots();
        }}
        confirmModalOpen={confirmModalOpen}
        onConfirmDeleteTimeSlots={handleConfirmDeleteTimeSlots}
        onCancelDeleteTimeSlots={handleCancelDeleteTimeSlots}
        showUpdateConfirmModal={showUpdateConfirmModal}
        hasPendingFormData={Boolean(pendingFormData)}
        onDismissUpdateConfirm={() => {
          setShowUpdateConfirmModal(false);
          setPendingFormData(null);
        }}
        onUpdateConfirm={handleUpdateConfirm}
        opportunityTitle={
          opportunityData
            ? {
                title_en: opportunityData.title_en || "",
                title_ar: opportunityData.title_ar || "",
              }
            : undefined
        }
      />
      <div className="border-t border-[#000] filterpage">
        <div className="compact-publish-form w-[90%] max-w-[1100px] mx-auto py-6 md:py-8">
          <h2>
            <Title text={t("COMMON.LEARN.SERVE.FORM")} variant="default" />
          </h2>
          <LearnServeFormContent
            form={form}
            autoSetTimeOnClick={autoSetTimeOnClick}
          />
        </div>
      </div>
    </OpportunityFormBoundary>
  );
}
