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

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

function AdminEventOrderDetailAttachmentAddPage() {
  const { eoid } = useParams();
  const attachmentManager = useAttachmentManager();
  const eventOrderManager = useEventOrderManager();

  // Fetch event order details
  const fetchEntity = useCallback(async (eventOrderId, onUnauthorized) => {
    return await eventOrderManager.getEventOrderDetail(
      eventOrderId,
      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,
      );
    },
    [],
  );  

  // Configuration object
  const config = useMemo(
    () => ({
      entityId: eoid,
      entityType: "event order",
      ownershipType: ATTACHMENT_OWNERSHIP_TYPE.EVENT_ORDER,
      maxFileSize: 10 * 1024 * 1024, // 10MB

      fetchEntity,
      uploadAttachment,

      breadcrumbs: {
        items: [
          {
            label: "Dashboard",
            to: "/admin/dashboard",
            icon: ChartBarIcon,
          },
          {
            label: "Event Orders",
            to: "/admin/event-orders",
            icon: TicketIcon,
          },
          {
            label: "Detail (Attachments)",
            to: `/admin/event-order/${eoid}/attachments`,
            icon: ClipboardDocumentListIcon,
          },
          {
            label: "Add",
            icon: PlusCircleIcon,
            isActive: true,
          },
        ],
      },

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

      routes: {
        backPath: `/admin/event-order/${eoid}/attachments`,
        backLabel: "Back to Attachments",
        successPath: `/admin/event-order/${eoid}/attachments`,
      },

      showEntityStatus: true,
      canUpload: (eventOrder) => {
        return eventOrder
          ? eventOrder.status !== 2 && !eventOrder.isBanned
          : false;
      },
    }),
    [eoid, fetchEntity, uploadAttachment],
  );

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

export default AdminEventOrderDetailAttachmentAddPage;
