// providers/deepseek.provider.ts
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import OpenAI from 'openai';
import { IAIProvider, AIMessage, AICompletionOptions, AICompletionResult } from '../interfaces/ai-provider.interface';

/**
 * DeepSeek dùng OpenAI-compatible API, giá cực rẻ (~$0.14/1M input).
 * Lý tưởng cho reviewer.
 */
@Injectable()
export class DeepSeekProvider implements IAIProvider {
    readonly name = 'deepseek';
    private client: OpenAI;
    private defaultModel: string;

    constructor(private config: ConfigService) {
        this.client = new OpenAI({
            apiKey: this.config.get('DEEPSEEK_API_KEY'),
            baseURL: 'https://api.deepseek.com/v1',
        });
        this.defaultModel = this.config.get('DEEPSEEK_MODEL', 'deepseek-chat');
    }

    async complete(messages: AIMessage[], options: AICompletionOptions = {}): Promise<AICompletionResult> {
        console.log(`DeepSeekProvider_deepseek`);
        const model = options.model ?? this.defaultModel;
        const res = await this.client.chat.completions.create({
            model,
            messages,
            temperature: options.temperature ?? 0.6,
            max_tokens: options.maxTokens ?? 400,
        });
        return {
            content: res.choices[0]?.message?.content?.trim() ?? '',
            tokensUsed: res.usage?.total_tokens ?? 0,
            model,
        };
    }
}
