import { AutotaskClient } from './autotask-client';
import { AutotaskConfig } from '@/lib/types/autotask';

let clientInstance: AutotaskClient | null = null;

export function getAutotaskClient(): AutotaskClient {
  if (!clientInstance) {
    const config: AutotaskConfig = {
      apiUrl: process.env.AUTOTASK_API_URL || '',
      username: process.env.AUTOTASK_USERNAME || '',
      password: process.env.AUTOTASK_SECRET || '',
      apiIntegrationCode: process.env.AUTOTASK_API_INTEGRATION_CODE || '',
    };

    // Validate configuration
    if (!config.apiUrl || !config.username || !config.password || !config.apiIntegrationCode) {
      throw new Error(
        'Missing Autotask API configuration. Please check your environment variables.'
      );
    }

    clientInstance = new AutotaskClient(config);
  }

  return clientInstance;
}
