import * as plugins from '../plugins.js';

/**
 * Configuration options for NetworkProxy
 */
export interface INetworkProxyOptions {
  port: number;
  maxConnections?: number;
  keepAliveTimeout?: number;
  headersTimeout?: number;
  logLevel?: 'error' | 'warn' | 'info' | 'debug';
  cors?: {
    allowOrigin?: string;
    allowMethods?: string;
    allowHeaders?: string;
    maxAge?: number;
  };
  
  // Settings for PortProxy integration
  connectionPoolSize?: number; // Maximum connections to maintain in the pool to each backend
  portProxyIntegration?: boolean; // Flag to indicate this proxy is used by PortProxy
  useExternalPort80Handler?: boolean; // Flag to indicate using external Port80Handler
  
  // ACME certificate management options
  acme?: {
    enabled?: boolean;              // Whether to enable automatic certificate management
    port?: number;                  // Port to listen on for ACME challenges (default: 80)
    contactEmail?: string;          // Email for Let's Encrypt account 
    useProduction?: boolean;        // Whether to use Let's Encrypt production (default: false for staging)
    renewThresholdDays?: number;    // Days before expiry to renew certificates (default: 30)
    autoRenew?: boolean;            // Whether to automatically renew certificates (default: true)
    certificateStore?: string;      // Directory to store certificates (default: ./certs)
    skipConfiguredCerts?: boolean;  // Skip domains that already have certificates configured
  };
}

/**
 * Interface for a certificate entry in the cache
 */
export interface ICertificateEntry {
  key: string;
  cert: string;
  expires?: Date;
}

/**
 * Interface for reverse proxy configuration
 */
export interface IReverseProxyConfig {
  destinationIps: string[];
  destinationPorts: number[];
  hostName: string;
  privateKey: string;
  publicKey: string;
  authentication?: {
    type: 'Basic';
    user: string;
    pass: string;
  };
  rewriteHostHeader?: boolean;
}

/**
 * Interface for connection tracking in the pool
 */
export interface IConnectionEntry {
  socket: plugins.net.Socket;
  lastUsed: number;
  isIdle: boolean;
}

/**
 * WebSocket with heartbeat interface
 */
export interface IWebSocketWithHeartbeat extends plugins.wsDefault {
  lastPong: number;
  isAlive: boolean;
}

/**
 * Logger interface for consistent logging across components
 */
export interface ILogger {
  debug(message: string, data?: any): void;
  info(message: string, data?: any): void;
  warn(message: string, data?: any): void;
  error(message: string, data?: any): void;
}

/**
 * Creates a logger based on the specified log level
 */
export function createLogger(logLevel: string = 'info'): ILogger {
  const logLevels = {
    error: 0,
    warn: 1,
    info: 2,
    debug: 3
  };
  
  return {
    debug: (message: string, data?: any) => {
      if (logLevels[logLevel] >= logLevels.debug) {
        console.log(`[DEBUG] ${message}`, data || '');
      }
    },
    info: (message: string, data?: any) => {
      if (logLevels[logLevel] >= logLevels.info) {
        console.log(`[INFO] ${message}`, data || '');
      }
    },
    warn: (message: string, data?: any) => {
      if (logLevels[logLevel] >= logLevels.warn) {
        console.warn(`[WARN] ${message}`, data || '');
      }
    },
    error: (message: string, data?: any) => {
      if (logLevels[logLevel] >= logLevels.error) {
        console.error(`[ERROR] ${message}`, data || '');
      }
    }
  };
}