const AWS = require('aws-sdk');
const axios = require('axios');
const mongoose = require('mongoose');
const FormData = require('form-data'); 
const Game = require('../../models/gameModel');
const aiEvaluateImageToStar = async (req, res) => {
  try {
    const { childId, gameName, gameID, gameTime, gameStar, screenShot } = req.body;
    if (!screenShot) {
      return res.status(400).json({ error: 'Screenshot is required' });
    }

        // Upload screenshot
    const formData = new FormData();
    formData.append('image', screenShot);
    
    const screenshotUploadResponse = await axios.post('https://teachertrainingchennai.in/api/uploadBase64/', formData, {
      headers: {
        ...formData.getHeaders(),
      },
      httpsAgent: new (require('https').Agent)({
        rejectUnauthorized: false
      })
    });

      
     const screenshotUrl = screenshotUploadResponse.data.filePath;
    
    // Generate random score between 1 to 10
    const randomScore = Math.floor(Math.random() * 10) + 1; // 1-10 range

    const gameData = new Game({
      gameName,
      childId,
      gameID,
      gameTime,
      gameStar: randomScore,
      screenshotUrl,
    });

    await gameData.save();

    return res.status(200).json({
      message: 'Game data saved successfully',
      gameStar: randomScore,
      screenshotUrl: screenshotUrl,
      data: gameData,
    });
  } catch (error) {
    console.error('Error:', error.message);
    return res.status(500).json({ error: error.message || 'Something went wrong' });
  }
};

module.exports = aiEvaluateImageToStar;
