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

import React, { useMemo, useCallback } from "react";
import { useParams } from "react-router";
import {
  ChartBarIcon,
  AcademicCapIcon,
  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 AdminInSchoolDetailAttachmentAddPage() {
  const { isid } = 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: isid,
      entityType: "in-school 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",
            icon: AcademicCapIcon,
          },
          {
            label: "In-School",
            to: "/admin/event/in-school",
            icon: AcademicCapIcon,
          },
          {
            label: "Detail (Attachments)",
            to: `/admin/event/in-school/${isid}/attachments`,
            icon: ClipboardDocumentListIcon,
          },
          {
            label: "Add",
            icon: PlusCircleIcon,
            isActive: true,
          },
        ],
      },

      header: {
        title: "In-School Event - Add Attachment",
        icon: DocumentArrowUpIcon,
        loadingText: "Loading event details...",
      },

      routes: {
        backPath: `/admin/event/in-school/${isid}/attachments`,
        backLabel: "Back to Attachments",
        successPath: `/admin/event/in-school/${isid}/attachments`,
      },

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

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

export default AdminInSchoolDetailAttachmentAddPage;
