const path = require("path");
const fs = require("fs");

const FaceModel = require("./model");
const MediaService = require("../../services/media.service");

// (Optional)
// const StudentModel = require("../students/model");

class FaceService {

    // =====================================================
    // Upload Face
    // =====================================================

    static async uploadFace(studentId, file, faceAngle = "unknown") {

        if (!file) {

            return {

                success: false,

                message: "No face image uploaded."

            };

        }

        // =============================================
        // Create Folder
        // =============================================

        const destinationFolder =
            MediaService.getFaceAngleFolder(
                studentId,
                faceAngle
            );

        // =============================================
        // Generate File Name
        // =============================================

        const extension =
            path.extname(file.originalname);

        const imageName =
            `face_${Date.now()}${extension}`;

        const finalPath =
            path.join(
                destinationFolder,
                imageName
            );

        // =============================================
        // Move Uploaded File
        // =============================================

        fs.renameSync(
            file.path,
            finalPath
        );

        // =============================================
        // Save Database
        // =============================================

        await FaceModel.addFace({

            student_id: studentId,

            image_name: imageName,

            image_path: finalPath,

            embedding_path: null,

            face_angle: faceAngle,

            quality_score: null,

            is_primary: false,

            status: "pending"

        });

        return {

            success: true,

            message: "Face uploaded successfully.",

            data: {

                image_name: imageName,

                image_path: finalPath

            }

        };

    }

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

    static async getStudentFaces(studentId) {

        const faces =
            await FaceModel.getStudentFaces(
                studentId
            );

        return {

            success: true,

            message: "Student face list.",

            data: faces

        };

    }

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

    static async deleteFace(faceId) {

        const face =
            await FaceModel.getFace(faceId);

        if (face.length === 0) {

            return {

                success: false,

                message: "Face image not found."

            };

        }

        // Delete Image

        MediaService.deleteFile(
            face[0].image_path
        );

        // Delete Embedding

        if (face[0].embedding_path) {

            MediaService.deleteFile(
                face[0].embedding_path
            );

        }

        await FaceModel.deleteFace(faceId);

        return {

            success: true,

            message: "Face deleted successfully."

        };

    }

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

    static async setPrimaryFace(studentId, faceId) {

        await FaceModel.setPrimary(

            studentId,

            faceId

        );

        return {

            success: true,

            message: "Primary face updated."

        };

    }

    // =====================================================
    // Mark Face as Trained
    // =====================================================

    static async updateEmbedding(

        faceId,

        embeddingPath

    ) {

        await FaceModel.updateEmbedding(

            faceId,

            embeddingPath

        );

        return {

            success: true,

            message: "Embedding updated."

        };

    }

}

module.exports = FaceService;