import { AddigyClient } from './addigy-client';
import { AddigyConfig } from '@/lib/types/addigy';

let cachedAddigyClient: AddigyClient | null = null;

export function getAddigyClient(): AddigyClient {
  if (cachedAddigyClient) {
    return cachedAddigyClient;
  }

  const config: AddigyConfig = {
    apiUrl: process.env.ADDIGY_API_URL || 'https://api.addigy.com/api/v2',
    apiToken: process.env.ADDIGY_API_TOKEN || '',
    organizationId: process.env.ADDIGY_ORG_ID,
  };

  // Validate required config
  if (!config.apiToken) {
    throw new Error('ADDIGY_API_TOKEN environment variable is required');
  }

  cachedAddigyClient = new AddigyClient(config);
  return cachedAddigyClient;
}

export function clearAddigyClientCache(): void {
  cachedAddigyClient = null;
}
