"use client";

import React from "react";
import UploadDocument from "@/components/ui/UploadDocument";
import type { FormikProps } from "formik";
import type { EventFormValues } from "./eventFormTypes";
import type { useEventForm } from "./useEventForm";

/** The same square crop for both pickers — it is what the cards render. */
const SQUARE_CROP = {
  enableCropping: true,
  cropAspectRatio: 1,
  cropShape: "rect",
  cropDisplayMode: "opportunity",
  cropWidth: 600,
  cropHeight: 600,
} as const;

/**
 * PDF p.22: the card image (mandatory) and the event images (optional), each
 * JPEG/PNG/WebP up to 10 MB — checked in `useEventForm`.
 */
export default function EventImagesField({
  screen,
  values,
  setFieldValue,
}: {
  screen: ReturnType<typeof useEventForm>;
  values: EventFormValues;
  setFieldValue: FormikProps<EventFormValues>["setFieldValue"];
}) {
  const {
    t,
    modifiedEventImages,
    existingCover,
    handleRemoveExistingFile,
  } = screen;

  return (
  <>
  <div className="event-card-image-field pb-4">
    <UploadDocument
      name="card_image"
      label={t("COMMON.EVENT_CARD_IMAGE")}
      accept="image/jpeg,image/png,image/webp"
      multiple
      /* One file, kept in an array like the gallery. */
      singleFileArray
      setFieldValue={setFieldValue}
      existingFiles={existingCover ? [{ id: 0, image: existingCover }] : []}
      onRemove={(index, isExisting) =>
        handleRemoveExistingFile(index, isExisting, "card_image", setFieldValue, values)
      }
      {...SQUARE_CROP}
    />
  </div>
  <div className="event-images-field pb-4">
    <UploadDocument
      name="event_images"
      label={t("COMMON.EVENT_GALLERY_IMAGES")}
      accept="image/jpeg,image/png,image/webp"
      multiple
      setFieldValue={setFieldValue}
      existingFiles={modifiedEventImages.map((file) => ({
        id: file.id,
        image: file.image,
      }))}
      onRemove={(index, isExisting) =>
        handleRemoveExistingFile(
          index,
          isExisting,
          "event_images",
          setFieldValue,
          values
        )
      }
      {...SQUARE_CROP}
    />
  </div>
  </>
  );
}
