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

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

function AdminEventContractDetailAttachmentAddPage() {
  const { ecid } = useParams();
  const attachmentManager = useAttachmentManager();
  const eventContractManager = useEventContractManager();

  // Fetch event contract details
  const fetchEntity = useCallback(async (eventContractId, onUnauthorized) => {
    return await eventContractManager.getEventContractDetail(
      eventContractId,
      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: ecid,
      entityType: "event contract",
      ownershipType: ATTACHMENT_OWNERSHIP_TYPE.EVENT_CONTRACT,
      maxFileSize: 10 * 1024 * 1024, // 10MB

      fetchEntity,
      uploadAttachment,

      breadcrumbs: {
        items: [
          {
            label: "Dashboard",
            to: "/admin/dashboard",
            icon: ChartBarIcon,
          },
          {
            label: "Event Contracts",
            to: "/admin/event-contracts",
            icon: DocumentTextIcon,
          },
          {
            label: "Detail (Attachments)",
            to: `/admin/event-contract/${ecid}/attachments`,
            icon: ClipboardDocumentListIcon,
          },
          {
            label: "Add",
            icon: PlusCircleIcon,
            isActive: true,
          },
        ],
      },

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

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

      showEntityStatus: true,
      canUpload: (eventContract) => {
        return eventContract ? eventContract.status !== 2 : false;
      },
    }),
    [ecid, fetchEntity, uploadAttachment],
  );

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

export default AdminEventContractDetailAttachmentAddPage;
