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

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

function AdminOrganizationDetailAttachmentAddPage() {
  const { oid } = useParams();
  const attachmentManager = useAttachmentManager();
  const organizationManager = useOrganizationManager();

  // Fetch organization details
  const fetchEntity = useCallback(async (organizationId, onUnauthorized) => {
    return await organizationManager.getOrganizationDetail(
      organizationId,
      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: oid,
      entityType: "organization",
      ownershipType: ATTACHMENT_OWNERSHIP_TYPE.ORGANIZATION,
      maxFileSize: 10 * 1024 * 1024, // 10MB

      fetchEntity,
      uploadAttachment,

      breadcrumbs: {
        items: [
          {
            label: "Dashboard",
            to: "/admin/dashboard",
            icon: ChartBarIcon,
          },
          {
            label: "Organizations",
            to: "/admin/organizations",
            icon: BuildingOffice2Icon,
          },
          {
            label: "Detail (Attachments)",
            to: `/admin/organization/${oid}/attachments`,
            icon: ClipboardDocumentListIcon,
          },
          {
            label: "Add",
            icon: PlusCircleIcon,
            isActive: true,
          },
        ],
      },

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

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

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

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

export default AdminOrganizationDetailAttachmentAddPage;
