/**
 * Logging utilities for GitOps
 */

type LogLevel = 'info' | 'success' | 'warn' | 'error' | 'debug';

class Logger {
  private debugMode = false;

  constructor() {
    this.debugMode = Deno.args.includes('--debug') || Deno.env.get('DEBUG') === 'true';
  }

  log(level: LogLevel, message: string, ...args: unknown[]): void {
    const prefix = this.getPrefix(level);
    const formattedMessage = `${prefix} ${message}`;

    switch (level) {
      case 'error':
        console.error(formattedMessage, ...args);
        break;
      case 'warn':
        console.warn(formattedMessage, ...args);
        break;
      case 'debug':
        if (this.debugMode) {
          console.log(formattedMessage, ...args);
        }
        break;
      default:
        console.log(formattedMessage, ...args);
    }
  }

  info(message: string, ...args: unknown[]): void {
    this.log('info', message, ...args);
  }

  success(message: string, ...args: unknown[]): void {
    this.log('success', message, ...args);
  }

  warn(message: string, ...args: unknown[]): void {
    this.log('warn', message, ...args);
  }

  error(message: string, ...args: unknown[]): void {
    this.log('error', message, ...args);
  }

  debug(message: string, ...args: unknown[]): void {
    this.log('debug', message, ...args);
  }

  private getPrefix(level: LogLevel): string {
    const colors: Record<LogLevel, string> = {
      info: '\x1b[36m',
      success: '\x1b[32m',
      warn: '\x1b[33m',
      error: '\x1b[31m',
      debug: '\x1b[90m',
    };
    const reset = '\x1b[0m';
    const icons: Record<LogLevel, string> = {
      info: 'i',
      success: '+',
      warn: '!',
      error: 'x',
      debug: '*',
    };
    return `${colors[level]}[${icons[level]}]${reset}`;
  }
}

export const logger = new Logger();
