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

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

function AdminSpeakerDetailAttachmentAddPage() {
  const { aid } = useParams();
  const attachmentManager = useAttachmentManager();
  const speakerManager = useSpeakerManager();

  // Fetch speaker details
  const fetchEntity = useCallback(async (speakerId, onUnauthorized) => {
    return await speakerManager.getSpeakerDetail(speakerId, 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: "speaker",
      ownershipType: ATTACHMENT_OWNERSHIP_TYPE.SPEAKER,
      maxFileSize: 10 * 1024 * 1024, // 10MB

      fetchEntity,
      uploadAttachment,

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

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

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

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

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

export default AdminSpeakerDetailAttachmentAddPage;
