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

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

function AdminDivisionDetailAttachmentAddPage() {
  const { did } = useParams();
  const attachmentManager = useAttachmentManager();
  const divisionManager = useDivisionManager();

  // Fetch division details
  const fetchEntity = useCallback(async (divisionId, onUnauthorized) => {
    return await divisionManager.getDivisionDetail(divisionId, 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: did,
      entityType: "division",
      ownershipType: ATTACHMENT_OWNERSHIP_TYPE.DIVISION,
      maxFileSize: 10 * 1024 * 1024, // 10MB

      fetchEntity,
      uploadAttachment,

      breadcrumbs: {
        items: [
          {
            label: "Dashboard",
            to: "/admin/dashboard",
            icon: ChartBarIcon,
          },
          {
            label: "Divisions",
            to: "/admin/divisions",
            icon: BuildingOfficeIcon,
          },
          {
            label: "Detail (Attachments)",
            to: `/admin/division/${did}/attachments`,
            icon: ClipboardDocumentListIcon,
          },
          {
            label: "Add",
            icon: PlusCircleIcon,
            isActive: true,
          },
        ],
      },

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

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

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

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

export default AdminDivisionDetailAttachmentAddPage;
