const db = require("../../config/db");

class FaceModel {

    // =====================================================
    // Add Face Image
    // =====================================================

    static addFace(data) {

        return new Promise((resolve, reject) => {

            const sql = `
                INSERT INTO student_faces
                (
                    student_id,
                    image_name,
                    image_path,
                    embedding_path,
                    face_angle,
                    quality_score,
                    is_primary,
                    status
                )
                VALUES (?, ?, ?, ?, ?, ?, ?, ?)
            `;

            db.query(

                sql,

                [

                    data.student_id,

                    data.image_name,

                    data.image_path,

                    data.embedding_path,

                    data.face_angle,

                    data.quality_score,

                    data.is_primary,

                    data.status

                ],

                (err, result) => {

                    if (err) {

                        return reject(err);

                    }

                    resolve(result);

                }

            );

        });

    }

    // =====================================================
    // Student Faces
    // =====================================================

    static getStudentFaces(studentId) {

        return new Promise((resolve, reject) => {

            db.query(

                `
                SELECT *
                FROM student_faces
                WHERE student_id=?
                ORDER BY created_at DESC
                `,

                [studentId],

                (err, result) => {

                    if (err)
                        return reject(err);

                    resolve(result);

                }

            );

        });

    }

    // =====================================================
    // Face By ID
    // =====================================================

    static getFace(faceId) {

        return new Promise((resolve, reject) => {

            db.query(

                `
                SELECT *
                FROM student_faces
                WHERE id=?
                `,

                [faceId],

                (err, result) => {

                    if (err)
                        return reject(err);

                    resolve(result);

                }

            );

        });

    }

    // =====================================================
    // Delete Face
    // =====================================================

    static deleteFace(faceId) {

        return new Promise((resolve, reject) => {

            db.query(

                `
                DELETE FROM student_faces
                WHERE id=?
                `,

                [faceId],

                (err, result) => {

                    if (err)
                        return reject(err);

                    resolve(result);

                }

            );

        });

    }

    // =====================================================
    // Update Embedding
    // =====================================================

    static updateEmbedding(faceId, embeddingPath) {

        return new Promise((resolve, reject) => {

            db.query(

                `
                UPDATE student_faces
                SET
                    embedding_path=?,
                    status='trained'
                WHERE id=?
                `,

                [

                    embeddingPath,

                    faceId

                ],

                (err, result) => {

                    if (err)
                        return reject(err);

                    resolve(result);

                }

            );

        });

    }

    // =====================================================
    // Set Primary Face
    // =====================================================

    static async setPrimary(studentId, faceId) {

        await new Promise((resolve, reject) => {

            db.query(

                `
                UPDATE student_faces
                SET is_primary=FALSE
                WHERE student_id=?
                `,

                [studentId],

                (err) => {

                    if (err)
                        return reject(err);

                    resolve();

                }

            );

        });

        return new Promise((resolve, reject) => {

            db.query(

                `
                UPDATE student_faces
                SET is_primary=TRUE
                WHERE id=?
                `,

                [faceId],

                (err, result) => {

                    if (err)
                        return reject(err);

                    resolve(result);

                }

            );

        });

    }

}

module.exports = FaceModel;