const AWS = require('aws-sdk');
const axios = require('axios');
const mongoose = require('mongoose');
const Game = require('../../models/gameModel'); // Ensure this path matches your project structure

const aiEvaluateImageToScore = async (req, res) => {
  try {
    // Destructure data from req.body
    const { userId, gameName, gameID, gameTime, gameStar, screenShot } = req.body;

    if (!screenShot) {
      return res.status(400).json({ error: 'Screenshot is required' });
    }

    // Decode the base64 image
    const buffer = Buffer.from(screenShot, 'base64');
    const fileName = `${Date.now()}-${gameID}.png`; // Unique filename
    const bucketName = process.env.S3_BUCKET_NAME;

    // Initialize S3
    AWS.config.update({
      region: 'ap-south-1',
      accessKeyId: process.env.AWS_ACCESS_KEY_ID,
      secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    });
    const s3 = new AWS.S3();

    // Upload the image to S3
    const s3Params = {
      Bucket: bucketName,
      Key: `screenshots/${fileName}`,
      Body: buffer,
      ContentType: 'image/png',
      ACL: 'public-read', // Make the file publicly accessible
    };

    const s3UploadResponse = await s3.upload(s3Params).promise();
    const screenshotUrl = s3UploadResponse.Location;

    // Prepare the OpenAI API request
    const openAiPayload = {
      model: 'gpt-4o-mini',
      messages: [
        {
          role: 'user',
          content: [
            {
              type: 'text',
              text: 'The coloring was done by a 5-year-old kid. Give a score between 1 to 10 in JSON format.',
            },
            {
              type: 'image_url',
              image_url: {
                url: screenshotUrl,
              },
            },
          ],
        },
      ],
      max_tokens: 300,
    };

    // Call the OpenAI API
    const openAiResponse = await axios.post(
      'https://api.openai.com/v1/chat/completions',
      openAiPayload,
      {
        headers: {
          'Content-Type': 'application/json',
          Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
        },
      }
    );

    // Extract the score from OpenAI's response
    const scoreResponse = openAiResponse.data.choices[0].message.content;
    const parsedScore = JSON.parse(scoreResponse);

    // Save to MongoDB
    const gameData = new Game({
      gameName,
      userId,
      gameID,
      gameTime,
      score: parsedScore.score || 'N/A', // Adjust key to match OpenAI response
      gameStar,
      screenshotUrl,
    });

    await gameData.save();

    // Respond with the saved data
    return res.status(201).json({
      message: 'Game data saved successfully',
      data: gameData,
    });
  } catch (error) {
    console.error('Error:', error.message);
    return res.status(500).json({ error: 'Something went wrong' });
  }
};

module.exports = aiEvaluateImageToScore;
