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

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

function AdminFacilitatorDetailAttachmentAddPage() {
  const { aid } = useParams();
  const attachmentManager = useAttachmentManager();
  const facilitatorManager = useFacilitatorManager();

  // Fetch facilitator details
  const fetchEntity = useCallback(async (facilitatorId, onUnauthorized) => {
    return await facilitatorManager.getFacilitatorDetail(facilitatorId, 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: aid,
    entityType: "facilitator",
    ownershipType: ATTACHMENT_OWNERSHIP_TYPE.FACILITATOR,
    maxFileSize: 10 * 1024 * 1024, // 10MB

    fetchEntity,
    uploadAttachment,

    breadcrumbs: {
      items: [
        {
          label: "Dashboard",
          to: "/admin/dashboard",
          icon: ChartBarIcon,
        },
        {
          label: "Facilitators",
          to: "/admin/facilitators",
          icon: UserGroupIcon,
        },
        {
          label: "Detail (Attachments)",
          to: `/admin/facilitator/${aid}/attachments`,
          icon: ClipboardDocumentListIcon,
        },
        {
          label: "Add",
          icon: PlusCircleIcon,
          isActive: true,
        },
      ]
    },

    header: {
      title: "Facilitator - Add Attachment",
      icon: DocumentArrowUpIcon,
      loadingText: "Loading facilitator details...",
    },

    routes: {
      backPath: `/admin/facilitator/${aid}/attachments`,
      backLabel: "Back to Attachments",
      successPath: `/admin/facilitator/${aid}/attachments`,
    },

    showEntityStatus: true,
    canUpload: (facilitator) => {
      return facilitator ? facilitator.status !== 'archived' && !facilitator.isBanned : false;
    },
  }), [aid, fetchEntity, uploadAttachment]);

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

export default AdminFacilitatorDetailAttachmentAddPage;
