const aiMarkDrawing = async (req, res) => {

  
  const express = require('express');
// const fetch = require('node-fetch'); // For making HTTP requests
const path = require('path');
const fs = require('fs');

// Create an Express app
const app = express();
const port = 3000;

// Middleware to handle JSON and URL-encoded form data
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Replace with your OpenAI API key
const OPENAI_API_KEY = 'your-openai-api-key';

// Route to handle image upload and forward it to OpenAI API
app.post('/upload-image', async (req, res) => {
    try {
        // Check if the file is included in the request
        if (!req.headers['content-type']?.includes('multipart/form-data')) {
            return res.status(400).send({ message: 'No file uploaded' });
        }

        // Extract the raw body data (binary) from the request
        let imageBuffer = Buffer.alloc(0);

        // Collect the image data from the stream (raw body)
        req.on('data', chunk => {
            imageBuffer = Buffer.concat([imageBuffer, chunk]);
        });

        req.on('end', async () => {
            // Forward the image to OpenAI's API for scoring
            const score = await sendImageToOpenAI(imageBuffer);

            // Respond with the score
            res.json({ score: score });
        });

    } catch (error) {
        console.error('Error processing the image:', error);
        res.status(500).send({ message: 'Internal Server Error' });
    }
});

// Function to send image to OpenAI API and get the response
async function sendImageToOpenAI(imageBuffer) {
    try {
        // Convert binary image data to base64 format
        const imageBase64 = imageBuffer.toString('base64');

        // Make a request to the OpenAI API using fetch
        const response = await fetch('https://api.openai.com/v1/chat/completions', {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${OPENAI_API_KEY}`,
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                model: "gpt-4-vision", // Use the appropriate model
                messages: [
                    { role: "system", content: "You are a grading assistant for 2nd grade students." },
                    { role: "user", content: "Evaluate this student's drawing or coloring." }
                ],
                functions: {
                    function_call: {
                        name: "vision_function",
                        args: {
                            image: imageBase64 // Send the image in base64 format
                        }
                    }
                }
            })
        });

        // Parse the response
        const data = await response.json();

        // Extract the score or other relevant information from the response
        const aiResponse = data.choices[0].message.content;

        // You can parse this response and extract the score, for example:
        const score = extractScoreFromResponse(aiResponse);
        return score;

    } catch (error) {
        console.error('Error interacting with OpenAI API:', error);
        throw error;
    }
}

// Example function to extract a score from OpenAI's response (you need to define this based on the actual response structure)
function extractScoreFromResponse(responseText) {
    // You can use a regex or logic to extract the score from OpenAI's response
    const scoreMatch = responseText.match(/score:\s*(\d+)/i);
    if (scoreMatch) {
        return parseInt(scoreMatch[1], 10);
    } else {
        // If no score is found, return a default value
        return 0;
    }
}

// Start the server
app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}`);
});


}

module.exports = aiMarkDrawing;