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

import React from "react";
import {
  BuildingOffice2Icon,
  CheckCircleIcon,
} from "@heroicons/react/24/outline";
import {
  useOrganizationManager,
} from "../../../../../../services/Services";
import { EntityActionConfirmationPage } from "../../../../../../components/business/views";

function AdminOrganizationDetailMoreUnbanPage() {
  const organizationManager = useOrganizationManager();

  // Get organization detail function
  const getOrganizationDetail = async (organizationId, onUnauthorized) => {
    return new Promise((resolve, reject) => {
      organizationManager.getOrganizationDetailWithCallbacks(
        organizationId,
        (organizationData) => {
          // Check if organization is actually banned
          if (!organizationData.isBanned) {
            reject(new Error("This organization is not banned and cannot be unbanned."));
            return;
          }
          resolve(organizationData);
        },
        (error) => reject(error),
        () => {},
        onUnauthorized
      );
    });
  };

  // Execute unban action
  const onUnbanExecute = async (organizationId, onUnauthorized) => {
    return new Promise((resolve, reject) => {
      const banData = {
        organization_id: organizationId,
      };

      organizationManager.banOrganizationWithCallbacks(
        banData,
        (response) => resolve(response),
        (error) => reject(error),
        () => {},
        onUnauthorized
      );
    });
  };

  return (
    <EntityActionConfirmationPage
      entityIcon={BuildingOffice2Icon}
      entityType="Organization"
      entityTypePlural="Organizations"
      basePath="/admin/organization"
      entityParamName="id"
      entityManager={organizationManager}
      getEntityDetail={getOrganizationDetail}
      actionIcon={CheckCircleIcon}
      actionName="Unban"
      actionDescription="Remove ban from this organization and restore full system access."
      actionWarningPoints={[
        "The organization will regain full access to the system",
        "All members will be able to log in again",
        "The ban warning will be removed from their profile",
        "They will be able to place and manage work orders",
        "All previous permissions will be restored"
      ]}
      actionButtonText="Remove Ban"
      actionButtonVariant="success"
      onActionExecute={onUnbanExecute}
      successMessage="Organization ban has been removed successfully!"
      redirectPath={null} // Stay on more page to see updated status
    />
  );
}

export default AdminOrganizationDetailMoreUnbanPage;