import { VeeamClient, VeeamClientConfig } from './veeam-client';

let veeamClientInstance: VeeamClient | null = null;

/**
 * Check if Veeam VSPC credentials are configured
 */
export function isVeeamConfigured(): boolean {
  return !!(process.env.VEEAM_VSPC_URL && process.env.VEEAM_VSPC_API_KEY);
}

/**
 * Get or create Veeam VSPC client singleton instance
 */
export function getVeeamClient(): VeeamClient {
  if (!veeamClientInstance) {
    const config: VeeamClientConfig = {
      baseUrl: process.env.VEEAM_VSPC_URL || '',
      apiKey: process.env.VEEAM_VSPC_API_KEY || '',
    };

    // Validate configuration
    if (!config.baseUrl || !config.apiKey) {
      throw new Error(
        'Veeam VSPC API credentials missing. Please set VEEAM_VSPC_URL and VEEAM_VSPC_API_KEY environment variables.'
      );
    }

    veeamClientInstance = new VeeamClient(config);
    console.log('Veeam VSPC client initialized');
  }

  return veeamClientInstance;
}

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