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

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

function AdminDivisionDetailMoreUnbanPage() {
  const divisionManager = useDivisionManager();

  // Get division detail function
  const getDivisionDetail = async (divisionId, onUnauthorized) => {
    return new Promise((resolve, reject) => {
      divisionManager.getDivisionDetailWithCallbacks(
        divisionId,
        (divisionData) => {
          // Check if division is actually banned
          if (!divisionData.isBanned) {
            reject(new Error("This division is not banned and cannot be unbanned."));
            return;
          }
          resolve(divisionData);
        },
        (error) => reject(error),
        () => {},
        onUnauthorized
      );
    });
  };

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

      divisionManager.unbanDivisionWithCallbacks(
        banData,
        (response) => resolve(response),
        (error) => reject(error),
        () => {},
        onUnauthorized
      );
    });
  };

  return (
    <EntityActionConfirmationPage
      entityIcon={BuildingOffice2Icon}
      entityType="Division"
      entityTypePlural="Divisions"
      basePath="/admin/division"
      entityParamName="id"
      entityManager={divisionManager}
      getEntityDetail={getDivisionDetail}
      actionIcon={CheckCircleIcon}
      actionName="Unban"
      actionDescription="Remove ban from this division and restore full system access."
      actionWarningPoints={[
        "The division 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="Division ban has been removed successfully!"
      redirectPath={null} // Stay on more page to see updated status
    />
  );
}

export default AdminDivisionDetailMoreUnbanPage;
