"use client";

import { Formik, Form } from "formik";
import OrganizerAccountSection from "./OrganizerAccountSection";
import OrganizerDetailsSection from "./OrganizerDetailsSection";
import OrganizerLicenseSection from "./OrganizerLicenseSection";
import OrganizerSocialSection from "./OrganizerSocialSection";
import OrganizerFormActions from "./OrganizerFormActions";
import type { useOrganizerAccountForm } from "./useOrganizerAccountForm";

/**
 * `<Formik>` and its render prop for the organization account form.
 *
 * Takes the assembled screen state as one prop rather than thirty: everything in
 * it comes from `useOrganizerAccountForm`, so listing the pieces individually
 * would be a second, drifting copy of that hook's return type.
 */
export default function OrganizerAccountForm({
  screen,
}: {
  screen: ReturnType<typeof useOrganizerAccountForm>;
}) {
  const {
    initialValues,
    validationSchema,
    handleSubmit,
    isUpdatingAccount,
    isUpdatingProfile,
    isUpdatingDocuments,
    isProfilePicUpdating,
    profilePic,
    uploadError,
    handleFileSelection,
    fileInputRef,
    nicknameAvailability,
    checkNicknameAvailability,
    accountData,
    organizerProfile,
    orgTypeOptions,
    orgTypeLoading,
    tagOptions,
    tagsLoading,
    keptDocIds,
    setKeptDocIds,
  } = screen;

  const originalDocCount = organizerProfile?.data?.documents?.length ?? 0;


  return (
          <Formik
            initialValues={initialValues}
            validationSchema={validationSchema}
            onSubmit={handleSubmit}
            enableReinitialize
            validateOnBlur
            validateOnChange
            validateOnMount={false}
          >
            {({
              values,
              setFieldValue,
              setFieldTouched,
              errors,
              isSubmitting: isFormSubmitting,
              touched,
            }) => {
              const isSocialMediaTouched =
                Array.isArray(touched.socialMedia) &&
                touched.socialMedia.some(
                  (field) => field?.platform || field?.link
                );

              const sectionProps = {
                values,
                errors,
                touched,
                setFieldValue,
                setFieldTouched,
                profilePic,
                isProfilePicUpdating,
                uploadError,
                handleFileSelection,
                fileInputRef,
                nicknameAvailability,
                checkNicknameAvailability,
                initialNickname: initialValues.nickname,
                accountData,
                organizerProfile,
                orgTypeOptions,
                orgTypeLoading,
                tagOptions,
                tagsLoading,
                keptDocIds,
                setKeptDocIds,
                originalDocCount,
                isSocialMediaTouched,
              };

              return (
                <div className="flex justify-center">
                  <div className="w-[90%] 2xl:w-[1200px] lg:w-[1000px] md:w-[90%] 2xl:pt-12 lg:pt-5 pt-5 relative">
                    <Form className="relative">
                      {/*
                        All four sections read from the same bag. Spreading it
                        keeps the four call sites from drifting apart — they take
                        identical props and always have.
                      */}
                      <OrganizerAccountSection {...sectionProps} />
                      <OrganizerDetailsSection {...sectionProps} />
                      <OrganizerLicenseSection {...sectionProps} />
                      <OrganizerSocialSection {...sectionProps} />

                      <OrganizerFormActions
                        isSubmitting={
                          isFormSubmitting ||
                          isUpdatingAccount ||
                          isUpdatingProfile ||
                          isUpdatingDocuments
                        }
                      />
                    </Form>
                  </div>
                </div>
              );
            }}
          </Formik>
  );
}
