// app/api/telegram/lib/openrouter-client.ts
import axios from 'axios';

export class OpenRouterClient {
    private baseURL = "https://openrouter.ai/api/v1";

    constructor(private apiKey: string, private model: string) { }

    async invoke(messages: any[]) { 
        try {
            const response = await axios.post(
                `${this.baseURL}/chat/completions`,
                {
                    model: this.model,
                    messages: messages.map(msg => ({
                        role: msg._getType() === 'human' ? 'user' : 'assistant',
                        content: msg.content
                    }))
                },
                {
                    headers: {
                        'Authorization': `Bearer ${this.apiKey}`,
                        'Content-Type': 'application/json',
                        'HTTP-Referer': process.env.APP_URL || 'http://localhost:3000',
                        'X-Title': 'Prop Chat Telegram Bot'
                    }
                }
            );
            return response.data.choices[0].message.content;
        } catch (error: any) {
            console.error('Error calling OpenRouter API:', error.response?.data || error.message);
            throw error;
        }
    }
}