import { Shutdown } from './shutdown';
import type { ShutdownResult } from './types';

/**
 * @stock-bot/shutdown - Shutdown management library
 *
 * Main exports for the shutdown library
 */

// Core shutdown classes and types
export { Shutdown } from './shutdown';
export type {
  ShutdownCallback,
  ShutdownOptions,
  ShutdownResult,
  PrioritizedShutdownCallback,
} from './types';

// Global singleton instance
let globalInstance: Shutdown | null = null;

/**
 * Get the global shutdown instance (creates one if it doesn't exist)
 */
function getGlobalInstance(): Shutdown {
  if (!globalInstance) {
    globalInstance = Shutdown.getInstance();
  }
  return globalInstance;
}

/**
 * Convenience functions for global shutdown management
 */

/**
 * Register a cleanup callback that will be executed during shutdown
 */
export function onShutdown(
  callback: () => Promise<void> | void,
  priority?: number,
  name?: string
): void {
  getGlobalInstance().onShutdown(callback, priority, name);
}

/**
 * Register a high priority shutdown callback (for queues, critical services)
 */
export function onShutdownHigh(callback: () => Promise<void> | void, name?: string): void {
  getGlobalInstance().onShutdownHigh(callback, name);
}

/**
 * Register a medium priority shutdown callback (for databases, connections)
 */
export function onShutdownMedium(callback: () => Promise<void> | void, name?: string): void {
  getGlobalInstance().onShutdownMedium(callback, name);
}

/**
 * Register a low priority shutdown callback (for loggers, cleanup)
 */
export function onShutdownLow(callback: () => Promise<void> | void, name?: string): void {
  getGlobalInstance().onShutdownLow(callback, name);
}

/**
 * Set the shutdown timeout in milliseconds
 */
export function setShutdownTimeout(timeout: number): void {
  getGlobalInstance().setTimeout(timeout);
}

/**
 * Check if shutdown is currently in progress
 */
export function isShuttingDown(): boolean {
  return globalInstance?.isShutdownInProgress() || false;
}

/**
 * Check if shutdown signal was received (for quick checks in running jobs)
 */
export function isShutdownSignalReceived(): boolean {
  const globalFlag = globalThis.__SHUTDOWN_SIGNAL_RECEIVED__ || false;
  const instanceFlag = globalInstance?.isShutdownSignalReceived() || false;
  return globalFlag || instanceFlag;
}

/**
 * Get the number of registered shutdown callbacks
 */
export function getShutdownCallbackCount(): number {
  return globalInstance?.getCallbackCount() || 0;
}

/**
 * Manually initiate graceful shutdown
 */
export function initiateShutdown(signal?: string): Promise<ShutdownResult> {
  return getGlobalInstance().shutdown(signal);
}

/**
 * Manually initiate graceful shutdown and exit the process
 */
export function shutdownAndExit(signal?: string, exitCode = 0): Promise<never> {
  return getGlobalInstance().shutdownAndExit(signal, exitCode);
}

/**
 * Reset the global instance (mainly for testing)
 */
export function resetShutdown(): void {
  globalInstance = null;
  Shutdown.reset();
}
