import { AuvikClient } from './auvik-client';
import { AuvikClientConfig } from '../types/auvik';

let auvikClientInstance: AuvikClient | null = null;

/**
 * Get or create Auvik client singleton instance
 */
export function getAuvikClient(): AuvikClient {
  if (!auvikClientInstance) {
    const config: AuvikClientConfig = {
      apiUrl: process.env.AUVIK_API_URL || '',
      apiUser: process.env.AUVIK_API_USER || '',
      apiKey: process.env.AUVIK_API_KEY || '',
    };

    // Validate configuration
    if (!config.apiUrl || !config.apiUser || !config.apiKey) {
      console.error('Auvik API credentials not configured');
      throw new Error(
        'Auvik API credentials missing. Please set AUVIK_API_URL, AUVIK_API_USER, and AUVIK_API_KEY environment variables.'
      );
    }

    auvikClientInstance = new AuvikClient(config);
    console.log('Auvik client initialized');
  }

  return auvikClientInstance;
}

/**
 * Reset the singleton instance (useful for testing)
 */
export function resetAuvikClient(): void {
  auvikClientInstance = null;
}
