// File Path: monorepo/web/frontend/src/pages/Admin/Event/FieldTrip/Detail/Attachment/Add/Page.jsx

import React, { useMemo, useCallback } from "react";
import { useParams } from "react-router";
import {
  ChartBarIcon,
  MapIcon,
  PlusCircleIcon,
  ClipboardDocumentListIcon,
  DocumentArrowUpIcon,
} from "@heroicons/react/24/outline";
import {
  useAttachmentManager,
  useEventManager,
} from "../../../../../../../services/Services";
import { EntityAttachmentAddPage } from "../../../../../../../components/UIX";
import { ATTACHMENT_OWNERSHIP_TYPE } from "../../../../../../../constants/Attachment";

function AdminFieldTripDetailAttachmentAddPage() {
  const { ftid } = useParams();
  const attachmentManager = useAttachmentManager();
  const eventManager = useEventManager();

  // Fetch event details
  const fetchEntity = useCallback(async (eventId, onUnauthorized) => {
    return await eventManager.getEventDetail(eventId, onUnauthorized);
  }, []); // eslint-disable-line react-hooks/exhaustive-deps

  // Handle attachment upload
  const uploadAttachment = useCallback(
    async (file, metadata, onProgress, onUnauthorized) => {
      return await attachmentManager.uploadAttachment(
        file,
        metadata,
        onProgress,
        onUnauthorized,
      );
    },
    [],
  ); // eslint-disable-line react-hooks/exhaustive-deps

  // Configuration object
  const config = useMemo(
    () => ({
      entityId: ftid,
      entityType: "field trip event",
      ownershipType: ATTACHMENT_OWNERSHIP_TYPE.EVENT,
      maxFileSize: 10 * 1024 * 1024, // 10MB

      fetchEntity,
      uploadAttachment,

      breadcrumbs: {
        items: [
          {
            label: "Dashboard",
            to: "/admin/dashboard",
            icon: ChartBarIcon,
          },
          {
            label: "Events",
            to: "/admin/events",
          },
          {
            label: "Field Trip",
            to: "/admin/event/field-trip",
            icon: MapIcon,
          },
          {
            label: "Detail (Attachments)",
            to: `/admin/event/field-trip/${ftid}/attachments`,
            icon: ClipboardDocumentListIcon,
          },
          {
            label: "Add",
            icon: PlusCircleIcon,
            isActive: true,
          },
        ],
      },

      header: {
        title: "Field Trip Event - Add Attachment",
        icon: DocumentArrowUpIcon,
        loadingText: "Loading field trip event details...",
      },

      routes: {
        backPath: `/admin/event/field-trip/${ftid}/attachments`,
        backLabel: "Back to Attachments",
        successPath: `/admin/event/field-trip/${ftid}/attachments`,
      },

      showEntityStatus: true,
      canUpload: (event) => {
        return event ? event.status !== "archived" : false;
      },
    }),
    [ftid, fetchEntity, uploadAttachment],
  );

  return <EntityAttachmentAddPage config={config} />;
}

export default AdminFieldTripDetailAttachmentAddPage;
