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

import React, { useMemo, useCallback } from "react";
import { useParams } from "react-router";
import {
  HomeIcon,
  VideoCameraIcon,
  PlusCircleIcon,
  PaperClipIcon,
  DocumentArrowUpIcon,
} from "@heroicons/react/24/outline";
import {
  useAttachmentManager,
  useEventManager,
} from "../../../../../../../services/Services";
import { EntityAttachmentAddPage } from "../../../../../../../components/UIX";
import { ATTACHMENT_OWNERSHIP_TYPE } from "../../../../../../../constants/Attachment";

function AdminVirtualDetailAttachmentAddPage() {
  const { vid } = useParams();
  const attachmentManager = useAttachmentManager();
  const eventManager = useEventManager();

  // Fetch virtual event details
  const fetchEntity = useCallback(async (eventId, onUnauthorized) => {
    return await eventManager.getEventDetail(eventId, 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: vid,
      entityType: "virtual event",
      ownershipType: ATTACHMENT_OWNERSHIP_TYPE.EVENT,
      maxFileSize: 10 * 1024 * 1024, // 10MB

      fetchEntity,
      uploadAttachment,

      breadcrumbs: {
        items: [
          {
            label: "Dashboard",
            to: "/admin/dashboard",
            icon: HomeIcon,
          },
          {
            label: "Events",
            to: "/admin/events",
            icon: VideoCameraIcon,
          },
          {
            label: "Virtual",
            to: "/admin/event/virtuals",
            icon: VideoCameraIcon,
          },
          {
            label: "Detail (Attachments)",
            to: `/admin/event/virtual/${vid}/attachments`,
            icon: PaperClipIcon,
          },
          {
            label: "Add",
            icon: PlusCircleIcon,
            isActive: true,
          },
        ],
      },

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

      routes: {
        backPath: `/admin/event/virtual/${vid}/attachments`,
        backLabel: "Back to Attachments",
        successPath: `/admin/event/virtual/${vid}/attachments`,
      },

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

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

export default AdminVirtualDetailAttachmentAddPage;
