import { ZoomClient, ZoomClientConfig } from './zoom-client';

let zoomClientInstance: ZoomClient | null = null;

/**
 * Check if Zoom Server-to-Server OAuth credentials are configured
 */
export function isZoomConfigured(): boolean {
  return !!(
    process.env.ZOOM_ACCOUNT_ID &&
    process.env.ZOOM_CLIENT_ID &&
    process.env.ZOOM_CLIENT_SECRET
  );
}

/**
 * Get or create Zoom client singleton
 */
export function getZoomClient(): ZoomClient {
  if (!zoomClientInstance) {
    const config: ZoomClientConfig = {
      accountId: process.env.ZOOM_ACCOUNT_ID || '',
      clientId: process.env.ZOOM_CLIENT_ID || '',
      clientSecret: process.env.ZOOM_CLIENT_SECRET || '',
    };

    if (!config.accountId || !config.clientId || !config.clientSecret) {
      throw new Error(
        'Zoom credentials missing. Set ZOOM_ACCOUNT_ID, ZOOM_CLIENT_ID, and ZOOM_CLIENT_SECRET.'
      );
    }

    zoomClientInstance = new ZoomClient(config);
    console.log('[ZOOM] Client initialized');
  }

  return zoomClientInstance;
}

export function resetZoomClient(): void {
  zoomClientInstance = null;
}
