import { IOGateway } from '@interopio/gateway';
import type { AddressInfo } from 'node:net';
import type { ServerConfig as GatewayServerConfig } from './types/config';

export default GatewayServer.Factory;

export namespace GatewayServer {
    export const VERSION: string;
    export const Factory: (options: ServerConfig) => Promise<Server>

    export import LoggerConfig = IOGateway.Logging.LogConfig;
    export type ServerConfig = GatewayServerConfig;

    export interface Server {
        readonly gateway: ScopedGateway
        /**
         * Returns the bound address info.
         * Useful when the server is bound to a random port.
         */
        readonly address: AddressInfo | null

        close(): Promise<void>
    }

    /**
     * A scoped gateway that extends the base Gateway interface with methods
     * to access and manage multiple gateway instances based on scope.
     * @since 0.20.0
     */
    export interface ScopedGateway extends IOGateway.Gateway {

        /**
         * Get gateway info.
         * @param gatewayId - The gateway ID. If omitted, returns aggregated info for all gateways.
         */
        info(gatewayId?: string): Record<string, unknown>

        /**
         * Stop a gateway instance.
         * @param gatewayId - The gateway ID to stop. If omitted, stops all gateways.
         */
        stop(gatewayId?: string): Promise<IOGateway.Gateway>;

        /**
         * Get all managed gateway instances.
         * @returns A map of gateway IDs to their corresponding Gateway instances
         */
        getGateways(): Map<string, IOGateway.Gateway>;
    }
}
