/**
 * AI Service Factory
 *
 * Provider selection via VITE_AI_PROVIDER:
 *
 *   backend  (default)  → BackendAIService
 *                          Calls POST /api/ai/chat/completions on the PowerOn backend.
 *                          The LLM API key is stored server-side only — not in this bundle.
 *                          In development: configure a Vite proxy (see vite.config.ts).
 *
 *   mock               → MockAIService
 *                          Deterministic responses, no network calls.
 *                          Use for local dev without a backend, or in CI.
 *
 * REMOVED: VITE_OPENROUTER_API_KEY and VITE_OPENROUTER_MODEL.
 * The OpenRouter key is now a server-side secret in PowerOn.
 */
import { MockAIService } from './mock/MockAIService'
import { BackendAIService } from './backend/BackendAIService'
import type { IAIService } from './IAIService'

function resolveProvider(): IAIService {
  const provider = import.meta.env.VITE_AI_PROVIDER as string | undefined

  if (provider === 'mock') {
    return MockAIService
  }

  if (provider === 'openrouter') {
    console.warn(
      '[aiService] VITE_AI_PROVIDER=openrouter is no longer supported. ' +
      'Direct OpenRouter calls have been removed from the frontend. ' +
      'Using BackendAIService (POST /api/ai/chat/completions) instead. ' +
      'Set VITE_AI_PROVIDER=backend or remove the variable to suppress this warning.',
    )
  }

  // Default: backend proxy. Falls back to mock automatically on network/HTTP errors.
  return BackendAIService
}

export const aiService: IAIService = resolveProvider()

export { MockAIService, BackendAIService }
// Compatibility alias for any code that still imports OpenRouterAIService by name.
export { OpenRouterAIService } from './openrouter/OpenRouterAIService'
export type { IAIService }
