"use client";

import { useTranslation } from "react-i18next";
import type { FormikErrors, FormikTouched } from "formik";
import DisabledButtonWithTooltip from "@/components/ui/DisabledButtonWithTooltip";
import TagsCheckbox from "@/components/ui/TagsCheckbox";
import LearnServeSponsorFields from "./LearnServeSponsorFields";
import LearnServeUploadFields from "./LearnServeUploadFields";
import type { LearnServeFormValues } from "./learnServeFormTypes";
import type { OpportunityMedia } from "@/features/opportunities/forms/useOpportunityMedia";

interface LearnServeAttachmentSectionsProps {
  values: LearnServeFormValues;
  errors: FormikErrors<LearnServeFormValues>;
  touched: FormikTouched<LearnServeFormValues>;
  setFieldValue: React.ComponentProps<typeof LearnServeUploadFields>["setFieldValue"];
  setFieldTouched: React.ComponentProps<typeof TagsCheckbox>["setFieldTouched"];
  media: OpportunityMedia;

  tagOptions: Array<{ id: string; label: string }>;
  tagsLoading: boolean;

  sponsorOptions: Array<{ label: string; value: string }>;
  sponsorsLoading: boolean;
  sponsorsFetching: boolean;
  handleMenuScroll: (event: React.UIEvent<HTMLDivElement>) => void;
  setSponsorSearch: (value: string) => void;
  rememberSponsor: (option: { value: string; label: string } | null) => void;

  /** Validates, then either submits or scrolls to the first error. */
  onSubmitClick: () => void;
  /** A save is in flight; the button must not start a second one. */
  isSaving: boolean;
  /** Decides the button's wording: Save, Repost, or Publish. */
  id?: string;
  isRepublish?: boolean;
}

/**
 * What is attached to the opportunity — interests, sponsors, images — and the
 * button that saves it.
 */
export default function LearnServeAttachmentSections({
  values,
  errors,
  touched,
  setFieldValue,
  setFieldTouched,
  media,
  tagOptions,
  tagsLoading,
  sponsorOptions,
  sponsorsLoading,
  sponsorsFetching,
  handleMenuScroll,
  setSponsorSearch,
  rememberSponsor,
  onSubmitClick,
  isSaving,
  id,
  isRepublish,
}: LearnServeAttachmentSectionsProps) {
  const { t } = useTranslation();
  const {
    modifiedOpportunityImages,
    modifiedLicenseImage,
    removeFile: handleRemoveExistingFile,
  } = media;

  return (
    <>
        <TagsCheckbox
          name="_interests"
          label={t("COMMON.ENTER_TAGS")}
          setFieldValue={setFieldValue}
          setFieldTouched={setFieldTouched}
          values={values._interests}
          errors={errors._interests as string | undefined}
          touched={touched._interests as boolean | undefined}
          options={tagOptions}
          disabled={tagsLoading}
        />

        <LearnServeSponsorFields
          values={values}
          errors={errors}
          touched={touched}
          sponsorOptions={sponsorOptions}
          sponsorsLoading={sponsorsLoading}
          sponsorsFetching={sponsorsFetching}
          setFieldValue={setFieldValue}
          handleMenuScroll={handleMenuScroll}
          setSponsorSearch={setSponsorSearch}
          rememberSponsor={rememberSponsor}
        />
        <LearnServeUploadFields
          setFieldValue={setFieldValue}
          values={values}
          modifiedOpportunityImages={modifiedOpportunityImages}
          modifiedLicenseImage={modifiedLicenseImage}
          isEditing={Boolean(id)}
          onRemoveExistingFile={handleRemoveExistingFile}
        />
        <div className="publish-form-actions flex justify-center gap-3 pt-6">
          <DisabledButtonWithTooltip
            type="button"
            variant="primary"
            size="medium"
            tooltipText=""
            disabled={isSaving}
            showTooltip={false}
            onClick={onSubmitClick}
            className="disabled:cursor-not-allowed disabled:opacity-50"
          >
            {id && !isRepublish
              ? t("COMMON.SAVE")
              : isRepublish
                ? t("COMMON.REPOST")
                : t("COMMON.PUBLISH")}
          </DisabledButtonWithTooltip>
        </div>
    </>
  );
}
