/**
 * ==================================================
 * Smart Attendance System
 * AI Service
 * ==================================================
 * Handles communication with the Python AI Engine.
 *
 * Responsibilities
 * ----------------
 * ✓ Health Check
 * ✓ Face Detection
 * ✓ Face Embedding
 * ✓ Face Training
 * ✓ Face Recognition
 *
 * Uses Multer Memory Storage.
 * No temporary files are created.
 * ==================================================
 */

const axios = require("axios");
const FormData = require("form-data");

const AI_BASE_URL =
    process.env.AI_BASE_URL ||
    "http://127.0.0.1:8000/api/v1";

class AIService {

    // ==========================================
    // Health Check
    // ==========================================

    static async health() {

        const response = await axios.get(

            `${AI_BASE_URL}/health`

        );

        return response.data;

    }

    // ==========================================
    // Face Detection
    // ==========================================

    static async detect(file) {

        const form = new FormData();

        form.append(

            "image",

            file.buffer,

            {

                filename:

                    file.originalname ||

                    "image.jpg",

                contentType:

                    file.mimetype

            }

        );

        const response = await axios.post(

            `${AI_BASE_URL}/detect`,

            form,

            {

                headers: form.getHeaders(),

                maxBodyLength: Infinity,

                maxContentLength: Infinity,

                timeout: 120000

            }

        );

        return response.data;

    }

    // ==========================================
    // Face Embedding
    // ==========================================

    static async embed(file) {

        const form = new FormData();

        form.append(

            "image",

            file.buffer,

            {

                filename:

                    file.originalname ||

                    "image.jpg",

                contentType:

                    file.mimetype

            }

        );

        const response = await axios.post(

            `${AI_BASE_URL}/embed`,

            form,

            {

                headers: form.getHeaders(),

                maxBodyLength: Infinity,

                maxContentLength: Infinity,

                timeout: 120000

            }

        );

        return response.data;

    }

    // ==========================================
    // Train Student
    // ==========================================

    static async train(

        studentId,

        files

    ) {

        const form = new FormData();

        form.append(

            "student_id",

            studentId

        );

        for (const file of files) {

            form.append(

                "images",

                file.buffer,

                {

                    filename:

                        file.originalname ||

                        "image.jpg",

                    contentType:

                        file.mimetype

                }

            );

        }

        const response = await axios.post(

            `${AI_BASE_URL}/train`,

            form,

            {

                headers: form.getHeaders(),

                maxBodyLength: Infinity,

                maxContentLength: Infinity,

                timeout: 300000

            }

        );

        return response.data;

    }

    // ==========================================
    // Recognize Student
    // ==========================================

    static async recognize(

        file,

        students

    ) {

        const form = new FormData();

        form.append(

            "image",

            file.buffer,

            {

                filename:

                    file.originalname ||

                    "image.jpg",

                contentType:

                    file.mimetype

            }

        );
    form.append(

    "students",

    JSON.stringify(

        students.map(student => ({

            student_id: student.student_id,

            embedding: student.average_embedding

        }))

    )

);

        const response = await axios.post(

            `${AI_BASE_URL}/recognize`,

            form,

            {

                headers: form.getHeaders(),

                maxBodyLength: Infinity,

                maxContentLength: Infinity,

                timeout: 300000

            }

        );

        return response.data;

    }

}

module.exports = AIService;