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

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

function AdminStaffDetailAttachmentAddPage() {
  const { aid } = useParams();
  const attachmentManager = useAttachmentManager();
  const staffManager = useStaffManager();

  // Fetch staff details
  const fetchEntity = useCallback(async (staffId, onUnauthorized) => {
    return await staffManager.getStaffDetail(staffId, 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: "staff member",
      ownershipType: ATTACHMENT_OWNERSHIP_TYPE.STAFF,
      maxFileSize: 10 * 1024 * 1024, // 10MB

      fetchEntity,
      uploadAttachment,

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

      header: {
        title: "Staff Member - Add Attachment",
        icon: DocumentArrowUpIcon,
        loadingText: "Loading staff details...",
      },

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

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

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

export default AdminStaffDetailAttachmentAddPage;
