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

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

function AdminOrganizationDetailMoreUnarchivePage() {
  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 archived
          if (organizationData.status !== 2) {
            reject(
              new Error(
                "This organization is not archived and cannot be unarchived.",
              ),
            );
            return;
          }
          resolve(organizationData);
        },
        (error) => reject(error),
        () => {},
        onUnauthorized,
      );
    });
  };

  // Execute unarchive action
  const onUnarchiveExecute = async (organizationId, onUnauthorized) => {
    return new Promise((resolve, reject) => {
      organizationManager.archiveOrganizationWithCallbacks(
        organizationId,
        (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={ArchiveBoxXMarkIcon}
      actionName="Unarchive"
      actionDescription="Restore this organization from archived status to active status."
      actionWarningPoints={[
        "This organization will become active again",
        "All associated users will regain access",
        "The organization will appear in active searches",
        "All previous settings and data will be restored",
        "Billing and subscription services may resume",
      ]}
      actionButtonText="Unarchive Organization"
      actionButtonVariant="success"
      onActionExecute={onUnarchiveExecute}
      successMessage="Organization successfully unarchived!"
      redirectPath="/admin/organizations"
    />
  );
}

export default AdminOrganizationDetailMoreUnarchivePage;
