"use client";

import { Form, Formik } from "formik";

import { isUnderage } from "./volunteerAccountRules";
import { useVolunteerAccountForm } from "./useVolunteerAccountForm";
import ProfilePictureCropGate from "./ProfilePictureCropGate";
import AccountFormGate from "./AccountFormGate";
import AccountFormActions from "./AccountFormActions";
import VolunteerAccountIdentitySection from "./VolunteerAccountIdentitySection";
import VolunteerAccountDetailsSection from "./VolunteerAccountDetailsSection";
import VolunteerGuardianSection from "./VolunteerGuardianSection";
import TrainerRequestSection from "@/features/trainer/components/TrainerRequestSection";
import { TRAINER_REQUEST_LIVE } from "@/features/trainer/trainerRequest";

/** Occupations that the design groups under a single "Employee" heading. */

export default function VolunteerAccountInformation() {
  const {
    t,
    authToken,
    uploadError,
    profilePic,
    isProfilePicUpdating,
    showCropModal,
    setShowCropModal,
    selectedImage,
    setSelectedImage,
    isUpdatingAccount,
    isUpdatingProfile,
    accountData,
    isAccountLoading,
    accountError,
    isProfileLoading,
    profileError,
    tagsLoading,
    genderLoading,
    relationshipLoading,
    tagOptions,
    genderOptions,
    relationshipOptions,
    nationalityOpts,
    healthConcernOpts,
    occupationOpts,
    getDefaultImage,
    initialValues,
    validationSchema,
    nicknameAvailability,
    checkNicknameAvailability,
    handleFileChange,
    handleCropComplete,
    handleSubmit,
    volunteerProfile,
  } = useVolunteerAccountForm();


  return (
    <AccountFormGate
      authToken={authToken}
      isLoading={isAccountLoading || isProfileLoading}
      error={accountError || profileError}
    >
      <div className="border-t border-[#000]">
        <ProfilePictureCropGate
          showCropModal={showCropModal}
          selectedImage={selectedImage}
          onCropComplete={handleCropComplete}
          setShowCropModal={setShowCropModal}
          setSelectedImage={setSelectedImage}
        />

        <div className="2xl:px-5 px-3 mobilescreen:px-[13px] 2xl:w-[75%] laptopmain:w-[83%] laptop:w-[78%] laptopitm:w-[85%] lg:w-[90%] md:w-[85%] w-[90%] mx-auto 2xl:py-[40px] laptopmain:py-[50px] laptop:py-[40px] lg:py-[40px] py-[40px]">
          <h2 className="font-bold 2xl:text-[50px] lg:text-[32px] text-[24px] text-primary-5">
            {t("COMMON.ACCOUNT_INFORMATION")}
          </h2>

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

              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">
                      <VolunteerAccountIdentitySection
                        values={values}
                        setFieldValue={setFieldValue}
                        profilePic={profilePic}
                        uploadError={uploadError}
                        handleFileChange={handleFileChange}
                        getDefaultImage={getDefaultImage}
                        nicknameAvailability={nicknameAvailability}
                        checkNicknameAvailability={checkNicknameAvailability}
                        accountData={accountData}
                      />
                      <VolunteerAccountDetailsSection
                        values={values}
                        errors={errors}
                        touched={touched}
                        setFieldValue={setFieldValue}
                        setFieldTouched={setFieldTouched}
                        isSocialMediaTouched={isSocialMediaTouched}
                        initialNickname={initialValues.nickname}
                        nicknameAvailability={nicknameAvailability}
                        checkNicknameAvailability={checkNicknameAvailability}
                        tagOptions={tagOptions}
                        tagsLoading={tagsLoading}
                        nationalityOpts={nationalityOpts}
                        healthConcernOpts={healthConcernOpts}
                        occupationOpts={occupationOpts}
                        relationshipOptions={relationshipOptions}
                        relationshipLoading={relationshipLoading}
                        genderOptions={genderOptions}
                        genderLoading={genderLoading}
                      />

                      <VolunteerGuardianSection
                        values={values}
                        setFieldValue={setFieldValue}
                        relationshipOptions={relationshipOptions}
                        relationshipLoading={relationshipLoading}
                        under18={under18}
                        accountData={accountData}
                      />
                      <AccountFormActions
                        isSubmitting={isFormSubmitting || isUpdatingAccount || isUpdatingProfile}
                      />
                    </Form>
                  </div>
                </div>
              );
            }}
          </Formik>
          {TRAINER_REQUEST_LIVE && (
            <div className="mx-auto w-[90%] 2xl:w-[720px] lg:w-[720px]">
              <TrainerRequestSection dob={volunteerProfile?.data?.dob} />
            </div>
          )}
        </div>
      </div>
    </AccountFormGate>
  );
}
