import fs from 'fs';
import dotenv from 'dotenv';
import { logger } from './logger';

dotenv.config();

const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const OPENAI_MODEL = process.env.OPENAI_MODEL;

async function callOpenAI(prompt: string): Promise<any> {
    if (!OPENAI_API_KEY || !OPENAI_MODEL) {
        throw new Error('OPENAI_API_KEY or OPENAI_MODEL is not defined in the .env file');
    }

    for (let i = 0; i < 10; i++) {
        let llmResponse = "";

        try {
            const response = await fetch('https://api.openai.com/v1/chat/completions', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${OPENAI_API_KEY}`,
                },
                body: JSON.stringify({
                    model: OPENAI_MODEL,
                    messages: [
                        {
                            role: 'user',
                            content: prompt,
                        },
                    ],
                    temperature: 0.7,
                }),
            });

            const data = await response.json();
            if (data.choices && data.choices.length > 0) {
                const content = data.choices[0].message.content;
                llmResponse = content;
                const jsonMatch = content.match(/\{[\s\S]*\}/);
                if (jsonMatch) {
                    return JSON.parse(jsonMatch[0]);
                } else {
                    const arrayMatch = content.match(/\[[\s\S]*\]/);
                    if (arrayMatch) {
                        return JSON.parse(arrayMatch[0]);
                    }
                }
            } else {
                logger.error('Unexpected API response:', data);
            }
        } catch (error) {
            logger.error(`Attempt ${i + 1} failed:`, error);
            logger.debug(`LLM response: ${llmResponse}`)
        }
    }

    throw new Error('Failed to get response from LLM after 10 attempts');
}

async function callOpenAIWithFile(imagePath: string, prompt: string): Promise<any> {
    if (!OPENAI_API_KEY || !OPENAI_MODEL) {
        throw new Error('OPENAI_API_KEY or OPENAI_MODEL is not defined in the .env file');
    }

    const imageBuffer = fs.readFileSync(imagePath);
    const base64Image = imageBuffer.toString('base64');

    for (let i = 0; i < 10; i++) {
        let llmResponse = "";

        try {
            const response = await fetch('https://api.openai.com/v1/chat/completions', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${OPENAI_API_KEY}`,
                },
                body: JSON.stringify({
                    model: OPENAI_MODEL,
                    messages: [
                        {
                            role: 'user',
                            content: [
                                { type: 'image_url', image_url: { url: `data:image/jpeg;base64,${base64Image}` } },
                                { type: 'text', text: prompt },
                            ],
                        },
                    ],
                    temperature: 0.7,
                }),
            });

            const data = await response.json();
            if (data.choices && data.choices.length > 0) {
                const content = data.choices[0].message.content;
                llmResponse = content;
                const jsonMatch = content.match(/\{[\s\S]*\}/);
                if (jsonMatch) {
                    return JSON.parse(jsonMatch[0]);
                } else {
                    const arrayMatch = content.match(/\[[\s\S]*\]/);
                    if (arrayMatch) {
                        return JSON.parse(arrayMatch[0]);
                    }
                }
            } else {
                logger.error('Unexpected API response:', data);
            }
        } catch (error) {
            logger.error(`Attempt ${i + 1} failed:`, error);
            logger.debug(`LLM response: ${llmResponse}`)
        }
    }

    throw new Error('Failed to describe image after 10 attempts');
}

export { callOpenAI, callOpenAIWithFile };
