/**
 * AI Provider Client Factories
 *
 * Provides lazy-initialized clients for OpenAI and Anthropic APIs.
 */

import Anthropic from '@anthropic-ai/sdk'
import OpenAI from 'openai'

// ============================================================================
// Anthropic Client
// ============================================================================

/**
 * Initialize Anthropic client
 */
export function getAnthropicClient(): Anthropic {
  const apiKey = process.env.ANTHROPIC_API_KEY
  if (!apiKey) {
    throw new Error('ANTHROPIC_API_KEY environment variable is not set')
  }
  return new Anthropic({ apiKey })
}

// ============================================================================
// OpenAI Client
// ============================================================================

// Singleton instance for connection reuse
let openaiClient: OpenAI | null = null

/**
 * Initialize OpenAI client (singleton)
 */
export function getOpenAIClient(): OpenAI {
  if (!openaiClient) {
    const apiKey = process.env.OPENAI_API_KEY
    if (!apiKey) {
      throw new Error('OPENAI_API_KEY environment variable is not set')
    }
    openaiClient = new OpenAI({ apiKey })
  }
  return openaiClient
}

// ============================================================================
// Pricing Constants
// ============================================================================

/**
 * GPT-5-mini pricing constants (per 1M tokens)
 */
export const GPT5_MINI_PRICING = {
  input: 0.25,      // $0.25 per 1M tokens
  cached: 0.025,    // $0.025 per 1M tokens (10% of input)
  output: 2.00,     // $2.00 per 1M tokens
}

/**
 * Claude 3.5 Haiku pricing constants (per 1M tokens)
 */
export const HAIKU_PRICING = {
  input: 0.80,       // $0.80 per 1M tokens
  cacheWrite: 1.00,  // $1.00 per 1M tokens (5m cache)
  cacheRead: 0.08,   // $0.08 per 1M tokens
  output: 4.00,      // $4.00 per 1M tokens
}

// ============================================================================
// Batching Configuration
// ============================================================================

/**
 * Number of files to include in each API call (Phase 2 optimization)
 * Batching multiple files reduces API overhead and leverages prompt caching better
 */
export const FILES_PER_API_BATCH = 8

/**
 * Number of API batches to process in parallel (Phase 3 optimization)
 * Higher values = faster scans but more API load
 * OpenAI/GPT-5-mini handles this well
 */
export const PARALLEL_API_BATCHES = 6
