"use client";

import { useTranslation } from "react-i18next";
import type { FormikProps, FormikValues } from "formik";
import UploadDocument from "@/components/ui/UploadDocument";

interface LearnServeUploadFieldsProps {
  setFieldValue: FormikProps<FormikValues>["setFieldValue"];
  values: FormikValues;
  /** Images already on the record, shown alongside anything newly picked. */
  modifiedOpportunityImages: Array<{ id: number; image: string }>;
  modifiedLicenseImage: Array<{ id: number; image: string }>;
  /** Editing or republishing an existing opportunity. */
  isEditing?: boolean;
  /** Removes one already-stored file, by index, from the two lists above. */
  onRemoveExistingFile: (
    index: number,
    isExisting: boolean,
    field: string,
    setFieldValue: (field: string, value: unknown) => void,
    values: FormikValues
  ) => void;
}

/**
 * The two file pickers: the opportunity's cover images, and the organizer's
 * licence where their account type requires one.
 *
 * Both crop to 1:1 — the ratio every card in the app renders — so what the
 * organizer frames here is exactly what appears on the listing.
 *
 * The licence picker is shown to everyone; whether it is *required* is a
 * validation rule (`isLicenseRequired`), not a rendering one, so an organizer
 * whose account type does not need one can still attach it.
 */
export default function LearnServeUploadFields({
  setFieldValue,
  values,
  modifiedOpportunityImages,
  modifiedLicenseImage,
  isEditing = false,
  onRemoveExistingFile,
}: LearnServeUploadFieldsProps) {
  const keptLicenseHidden =
    isEditing &&
    !values.license_image_removed &&
    modifiedLicenseImage.length === 0 &&
    !(values.license_image instanceof File);
  const { t } = useTranslation();

  return (
    <>
      <div className="pb-4">
        <UploadDocument
          name="opportunity_images"
          label={t("COMMON.UPLOAD_IMAGE")}
          accept="image/jpeg, image/png"
          multiple
          /* One image per opportunity — the picker, the previews and the
             submitted array are all capped at one. `multiple` stays so the
             value keeps its array shape; `singleFileArray` is the cap. */
          singleFileArray
          setFieldValue={setFieldValue}
          existingFiles={modifiedOpportunityImages.map((file) => ({
            id: file.id,
            image: file.image,
          }))}
          onRemove={(index, isExisting) =>
            onRemoveExistingFile(
              index,
              isExisting,
              "opportunity_images",
              setFieldValue,
              values
            )
          }
          enableCropping
          cropAspectRatio={1}
          cropShape="rect"
          cropDisplayMode="opportunity"
          cropWidth={600}
          cropHeight={600} // 1:1 — matches the square the cards render
        />
      </div>

      <div>
        <UploadDocument
          name="license_image"
          label={t("COMMON.UPLOAD_LICENSE_IMAGE")}
          multiple={false}
          accept="image/jpeg, image/png"
          setFieldValue={setFieldValue}
          existingFiles={modifiedLicenseImage.map((file) => ({
            id: file.id,
            image: file.image,
          }))}
          onRemove={(index, isExisting) =>
            onRemoveExistingFile(
              index,
              isExisting,
              "license_image",
              setFieldValue,
              values
            )
          }
          enableCropping={false} // License images are uploaded as-is
          onChange={(
            event: React.ChangeEvent<HTMLInputElement>
          ) => {
            event.preventDefault();
            const file = event.target.files
              ? event.target.files[0]
              : null;
            setFieldValue("license_image", file);
            // Picking a new file cancels a pending removal
            if (file) {
              setFieldValue("license_image_removed", false);
            }
          }}
        />
        {/* BE-111: the API hides the stored licence from its own organization,
            so the picker looks empty although the file is still kept. Said
            out loud so an edit doesn't read as "the licence was deleted". */}
        {/* {keptLicenseHidden && (
          <p className="-mt-2 mb-4 text-sm text-secondary-102">
            {t("COMMON.LICENSE_KEPT_ON_EDIT")}
          </p>
        )} */}
      </div>

    </>
  );
}
