"use client";

import type { TimeSlot } from "@/features/shared";
import LearnServeFormEffects from "./LearnServeFormEffects";
import LearnServeFieldGrid from "./LearnServeFieldGrid";
import LearnServeLocationFields from "./LearnServeLocationFields";
import LearnServeDescriptionFields from "./LearnServeDescriptionFields";
import type { LearnServeFormValues } from "./learnServeFormTypes";
import type { LearnServeOptions } from "./useLearnServeOptions";

interface LearnServeFieldSectionsProps {
  values: LearnServeFormValues;
  touched: Record<string, unknown>;
  setFieldValue: (field: string, value: unknown) => void;
  setFieldTouched: (field: string, touched: boolean) => void;
  validateForm: () => Promise<unknown>;
  options: LearnServeOptions;

  /** Derived from the values by the caller, so the rules stay in one place. */
  isOnline: boolean;
  isInternshipOrCourse: boolean;
  canBePaid: boolean;
  isPaid: boolean;

  showEnglishTitle: boolean;
  onAddEnglishTitle: () => void;
  onRemoveEnglishTitle: () => void;
  showEnglishDescription: boolean;
  onAddEnglishDescription: () => void;
  onRemoveEnglishDescription: () => void;
  showMapPicker: boolean;
  onUseMapPicker: () => void;
  onUseLocationLink: () => void;

  platformFeePercent: number | null;
  autoSetTimeOnClick: boolean;
  selectedLanguage: string;
  id?: string;
  opportunityData?: React.ComponentProps<typeof LearnServeFormEffects>["opportunityData"];
  timeSlotModalOpen: boolean;
  setInitialTimeSlots: (slots: TimeSlot[]) => void;
  skipNextGeocodeRef: { current: boolean };
}

/**
 * The opportunity's own details: what it is, when, where, and its description.
 *
 * Presentation only — every conditional here is decided by the caller and
 * arrives as a prop.
 */
export default function LearnServeFieldSections({
  values,
  touched,
  setFieldValue,
  setFieldTouched,
  validateForm,
  options,
  isOnline,
  isInternshipOrCourse,
  canBePaid,
  isPaid,
  showEnglishTitle,
  onAddEnglishTitle,
  onRemoveEnglishTitle,
  showEnglishDescription,
  onAddEnglishDescription,
  onRemoveEnglishDescription,
  showMapPicker,
  onUseMapPicker,
  onUseLocationLink,
  platformFeePercent,
  autoSetTimeOnClick,
  selectedLanguage,
  id,
  opportunityData,
  timeSlotModalOpen,
  setInitialTimeSlots,
  skipNextGeocodeRef,
}: LearnServeFieldSectionsProps) {
  return (
    <>
        <LearnServeFormEffects
          values={values}
          touched={touched as Record<string, unknown>}
          validateForm={validateForm}
          setFieldValue={setFieldValue}
          opportunityData={opportunityData}
          id={id}
          selectedLanguage={selectedLanguage}
          timeSlotModalOpen={timeSlotModalOpen}
          setInitialTimeSlots={setInitialTimeSlots}
          skipNextGeocodeRef={skipNextGeocodeRef}
        />

        {/*
          One four-column grid for the short fields, replacing the
          stack of flex rows this form used to be — the same grid
          `VolunteerForm` uses, and the order the client's mockup
          shows:
            1. title · type · format · participants needed
            2. due date · start date · end date · time from/to
            3. age · gender · meeting link · WhatsApp contact
            4. paid-or-free · price · certificate · nationality
          The pairs that read as one answer — age from/to and time
          from/to — each occupy a single column, which is what keeps
          the rows four wide. "Where" sits below the grid rather
          than in it, because the map picker is far taller than a
          field and would set the height of its whole row.
        */}
        <LearnServeFieldGrid
          values={values}
          setFieldValue={setFieldValue}
          setFieldTouched={setFieldTouched}
          showEnglishTitle={showEnglishTitle}
          onAddEnglishTitle={onAddEnglishTitle}
          onRemoveEnglishTitle={onRemoveEnglishTitle}
          isInternshipOrCourse={isInternshipOrCourse}
          isOnline={isOnline}
          canBePaid={canBePaid}
          isPaid={isPaid}
          autoSetTimeOnClick={autoSetTimeOnClick}
          platformFeePercent={platformFeePercent}
          options={options}
        />

        {/*
          Where an in-person opportunity happens. Hidden entirely
          for an online one, which has no physical location.
        */}
        {!isOnline && (
          <LearnServeLocationFields
            values={values}
            setFieldValue={setFieldValue}
            setFieldTouched={setFieldTouched}
            selectedLanguage={selectedLanguage}
            showMapPicker={showMapPicker}
            onUseMapPicker={onUseMapPicker}
            onUseLocationLink={onUseLocationLink}
            skipNextGeocodeRef={skipNextGeocodeRef}
          />
        )}

        <LearnServeDescriptionFields
          showEnglishDescription={showEnglishDescription}
          onAddEnglishDescription={onAddEnglishDescription}
          onRemoveEnglishDescription={onRemoveEnglishDescription}
        />
    </>
  );
}
