// File Path: monorepo/web/frontend/src/pages/Admin/EventOrder/Detail/More/Page.jsx

import React, { useCallback } from "react";
import {
  TicketIcon,
  EllipsisHorizontalIcon,
  ArchiveBoxIcon,
  ArchiveBoxXMarkIcon,
  TrashIcon,
  EyeIcon,
  DocumentTextIcon,
  ChatBubbleLeftRightIcon,
  PaperClipIcon,
  ChartBarIcon,
} from "@heroicons/react/24/outline";
import { useEventOrderManager } from "../../../../../services/Services";
import { EntityMorePage } from "../../../../../components/business/views";

// EventOrder constants
const EVENT_ORDER_STATUS_ACTIVE = 1;
const EVENT_ORDER_STATUS_ARCHIVED = 2;

const EVENT_ORDER_CONSTANTS = {
  EVENT_ORDER_STATUS_ACTIVE,
  EVENT_ORDER_STATUS_ARCHIVED,
};

// Function to create tab items for event order
const createEventOrderTabItems = (eventOrder, eventOrderId) => [
  {
    label: "Quick View",
    href: `/admin/event-order/${eventOrder.id}`,
    isActive: false,
    icon: EyeIcon,
  },
  {
    label: "Full Details",
    href: `/admin/event-order/${eventOrder.id}/full`,
    isActive: false,
    icon: DocumentTextIcon,
  },
  {
    label: "Comments",
    href: `/admin/event-order/${eventOrder.id}/comments`,
    isActive: false,
    icon: ChatBubbleLeftRightIcon,
  },
  {
    label: "Attachments",
    href: `/admin/event-order/${eventOrder.id}/attachments`,
    isActive: false,
    icon: PaperClipIcon,
  },
  {
    label: "More",
    href: `/admin/event-order/${eventOrder.id}/more`,
    isActive: true,
    icon: EllipsisHorizontalIcon,
  },
];

// Function to create action cards for event order
const createEventOrderActionCards = (eventOrder, eventOrderId, constants) => {
  const actionCards = [];

  if (eventOrder) {
    // Archive/Unarchive
    if (eventOrder.status === constants.EVENT_ORDER_STATUS_ARCHIVED) {
      actionCards.push({
        key: "unarchive",
        title: "Unarchive",
        subtitle: "Make event order visible in list and search results",
        icon: ArchiveBoxXMarkIcon,
        path: `/admin/event-order/${eventOrderId}/more/unarchive`,
        disabled: false,
      });
    } else {
      actionCards.push({
        key: "archive",
        title: "Archive",
        subtitle: "Hide event order from list and search results",
        icon: ArchiveBoxIcon,
        path: `/admin/event-order/${eventOrderId}/more/archive`,
        disabled: false,
      });
    }

    // Delete - For Executive/Management roles (EntityMorePage will handle role filtering)
    actionCards.push({
      key: "delete",
      title: "Delete",
      subtitle: "Permanently delete this event order and all related data",
      icon: TrashIcon,
      path: `/admin/event-order/${eventOrderId}/more/delete`,
      disabled: false,
    });
  }

  return actionCards;
};

function AdminEventOrderDetailMorePage() {
  const eventOrderManager = useEventOrderManager();

  // Custom get event order detail function wrapped with useCallback to prevent infinite loops
  const getEventOrderDetailWithManager = useCallback(
    async (eventOrderId, onUnauthorized) => {
      return await eventOrderManager.getEventOrderDetail(
        eventOrderId,
        onUnauthorized,
      );
    },
    [eventOrderManager],
  );

  return (
    <EntityMorePage
      entityIcon={TicketIcon}
      entityType="Event Order"
      entityTypePlural="Event Orders"
      basePath="/admin/event-orders"
      entityParamName="eventOrderID"
      entityManager={eventOrderManager}
      getEntityDetail={getEventOrderDetailWithManager}
      createTabItems={createEventOrderTabItems}
      createActionCards={createEventOrderActionCards}
      constants={EVENT_ORDER_CONSTANTS}
      infoMessage="Some actions may affect the event order's visibility or permanently remove data. Executive and Management roles have access to additional administrative actions."
    />
  );
}

export default AdminEventOrderDetailMorePage;
