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

import React, { useCallback, useMemo } from "react";
import {
  UserIcon,
  CameraIcon,
  ArchiveBoxIcon,
  ArchiveBoxXMarkIcon,
  TrashIcon,
  LockClosedIcon,
  DevicePhoneMobileIcon,
  NoSymbolIcon,
  CheckCircleIcon,
  ArrowUpCircleIcon,
  ArrowDownCircleIcon,
  EllipsisHorizontalIcon,
  ChartBarIcon,
} from "@heroicons/react/24/outline";
import {
  useCustomerManager,
  useAccountManager,
} from "../../../../../services/Services";
import { EntityMorePage } from "../../../../../components/business/views";
import {
  EXECUTIVE_ROLE_ID,
  MANAGEMENT_ROLE_ID,
} from "../../../../../constants/Roles";
import {
  RESIDENTIAL_CUSTOMER_TYPE_OF_ID,
  COMMERCIAL_CUSTOMER_TYPE_OF_ID,
  CUSTOMER_STATUS,
} from "../../../../../constants/Customer";

const CUSTOMER_CONSTANTS = {
  RESIDENTIAL_CUSTOMER_TYPE_ID: RESIDENTIAL_CUSTOMER_TYPE_OF_ID,
  COMMERCIAL_CUSTOMER_TYPE_ID: COMMERCIAL_CUSTOMER_TYPE_OF_ID,
  CUSTOMER_STATUS_ACTIVE: CUSTOMER_STATUS.ACTIVE,
  CUSTOMER_STATUS_ARCHIVED: CUSTOMER_STATUS.ARCHIVED,
};

// Memoized function to create tab items for customer
const createCustomerTabItems = (customer, customerId) => [
  {
    label: "Summary",
    href: `/admin/customer/${customer.id}`,
    isActive: false,
  },
  {
    label: "Detail",
    href: `/admin/customer/${customer.id}/detail`,
    isActive: false,
  },
  {
    label: "Event Orders",
    href: `/admin/customer/${customer.id}/event-orders`,
    isActive: false,
  },
  {
    label: "Comments",
    href: `/admin/customer/${customer.id}/comments`,
    isActive: false,
  },
  {
    label: "Attachments",
    href: `/admin/customer/${customer.id}/attachments`,
    isActive: false,
  },
  {
    label: "More",
    href: `/admin/customer/${customer.id}/more`,
    isActive: true,
    icon: EllipsisHorizontalIcon,
  },
];

// Memoized function to create action cards for customer
const createCustomerActionCards = (customer, customerId, constants) => {
  const actionCards = [];

  if (customer) {
    // Note: Role-based filtering will be handled by the parent component
    // For now, we'll include all actions and let EntityMorePage handle permissions
    const isExecutiveOrManagement = true; // Will be filtered by component logic

    // Avatar - Available for all customers
    actionCards.push({
      key: "avatar",
      title: "Avatar",
      subtitle: "Change the customer's profile picture",
      icon: CameraIcon,
      path: `/admin/customer/${customerId}/more/avatar`,
      disabled: false,
    });

    // Archive/Unarchive
    if (customer.status === CUSTOMER_STATUS.ARCHIVED) {
      actionCards.push({
        key: "unarchive",
        title: "Unarchive",
        subtitle: "Make customer visible in list and search results",
        icon: ArchiveBoxXMarkIcon,
        path: `/admin/customer/${customerId}/unarchive`,
        disabled: false,
      });
    } else {
      actionCards.push({
        key: "archive",
        title: "Archive",
        subtitle: "Make customer hidden from list and search results",
        icon: ArchiveBoxIcon,
        path: `/admin/customer/${customerId}/archive`,
        disabled: false,
      });
    }

    // Upgrade/Downgrade - Only for active customers
    if (customer.status === CUSTOMER_STATUS.ACTIVE) {
      if (customer.type === COMMERCIAL_CUSTOMER_TYPE_OF_ID) {
        actionCards.push({
          key: "downgrade",
          title: "Downgrade",
          subtitle: "Change customer to become residential customer",
          icon: ArrowDownCircleIcon,
          path: `/admin/customer/${customerId}/downgrade`,
          disabled: false,
        });
      } else {
        actionCards.push({
          key: "upgrade",
          title: "Upgrade",
          subtitle: "Change customer to become business customer",
          icon: ArrowUpCircleIcon,
          path: `/admin/customer/${customerId}/upgrade`,
          disabled: false,
        });
      }

      // Password - Only for Executive/Management and active customers
      if (isExecutiveOrManagement) {
        actionCards.push({
          key: "password",
          title: "Password",
          subtitle: "Change or reset the user's password",
          icon: LockClosedIcon,
          path: `/admin/customer/${customerId}/change-password`,
          disabled: false,
        });

        // 2FA - Only for Executive/Management and active customers
        actionCards.push({
          key: "2fa",
          title: "2FA",
          subtitle: "Enable or disable two-factor authentication",
          icon: DevicePhoneMobileIcon,
          path: `/admin/customer/${customerId}/change-2fa`,
          disabled: false,
        });

        // Delete - Only for Executive/Management and active customers
        actionCards.push({
          key: "delete",
          title: "Delete",
          subtitle: "Permanently delete this customer and all related data",
          icon: TrashIcon,
          path: `/admin/customer/${customerId}/permadelete`,
          disabled: false,
        });
      }
    }

    // Ban/Unban
    if (customer.isBanned) {
      actionCards.push({
        key: "unban",
        title: "Unban",
        subtitle: "Remove ban from customer",
        icon: CheckCircleIcon,
        path: `/admin/customer/${customerId}/unban`,
        disabled: false,
      });
    } else {
      actionCards.push({
        key: "ban",
        title: "Ban",
        subtitle: "Mark the customer as banned",
        icon: NoSymbolIcon,
        path: `/admin/customer/${customerId}/ban`,
        disabled: false,
      });
    }
  }

  return actionCards;
};

function AdminCustomerDetailMorePage() {
  const customerManager = useCustomerManager();

  // Custom get customer detail function wrapped with useCallback to prevent infinite loops
  const getCustomerDetailWithManager = useCallback(
    async (customerId, onUnauthorized) => {
      return await customerManager.getCustomerDetail(
        customerId,
        onUnauthorized,
      );
    },
    [customerManager],
  );

  return (
    <EntityMorePage
      entityIcon={UserIcon}
      entityType="Customer"
      entityTypePlural="Customers"
      basePath="/admin/customers"
      entityParamName="cid"
      entityManager={customerManager}
      getEntityDetail={getCustomerDetailWithManager}
      createTabItems={createCustomerTabItems}
      createActionCards={createCustomerActionCards}
      constants={CUSTOMER_CONSTANTS}
      infoMessage="Some actions are only available for active customers. Archived customers must be unarchived first before performing other actions. Executive and Management roles have access to additional administrative actions."
    />
  );
}

export default AdminCustomerDetailMorePage;
