import OpenAI from 'openai';

/**
 * AI Provider configuration
 * 
 * Set AI_PROVIDER in .env.local to switch:
 * - "openrouter" (default) - Uses OpenRouter with Gemma 3 27B via Nebius/FP8
 * - "gemini" - Uses Google Gemini 2.5 Flash
 */
export type AIProvider = 'openrouter';

export function getAIProvider(): AIProvider {
    return 'openrouter';
}

/**
 * OpenRouter client for vision tasks
 * Uses OpenAI-compatible API
 */
export function getOpenRouterClient(): OpenAI {
    const apiKey = process.env.OPENROUTER_API_KEY;

    if (!apiKey) {
        throw new Error('OPENROUTER_API_KEY is not configured.');
    }

    return new OpenAI({
        baseURL: 'https://openrouter.ai/api/v1',
        apiKey: apiKey,
        defaultHeaders: {
            'HTTP-Referer': 'https://whiskyvault.app',
            'X-Title': 'WhiskyVault',
        },
    });
}

// Default OpenRouter model for vision tasks
export const OPENROUTER_VISION_MODEL = 'google/gemma-3-27b-it';
//export const OPENROUTER_VISION_MODEL = 'google/gemma-3n-e4b-it';

/**
 * OpenRouter provider preferences
 * - Prioritize Nebius provider for better availability
 * - Request FP8 quantization for quality/speed balance
 */
export const OPENROUTER_PROVIDER_PREFERENCES = {
    order: ['Nebius'],           // Prioritize Nebius
    quantizations: ['fp8'],      // Use FP8 for quality
    allow_fallbacks: true,       // Allow fallback to other providers
};
