const FaceService = require("./service");
const Response = require("../../utils/response");

class FaceController {

    // =====================================================
    // Upload Face
    // POST /api/faces/upload/:studentId
    // =====================================================

    static async uploadFace(req, res, next) {

        try {

            const studentId = req.params.studentId;

            const faceAngle =
                req.body.face_angle || "unknown";

            const result =
                await FaceService.uploadFace(

                    studentId,

                    req.file,

                    faceAngle

                );

            if (!result.success) {

                return res.status(400).json(

                    Response.error(result.message)

                );

            }

            return res.status(201).json(

                Response.success(

                    result.message,

                    result.data

                )

            );

        }
        catch (error) {

            next(error);

        }

    }

    // =====================================================
    // Student Faces
    // GET /api/faces/student/:studentId
    // =====================================================

    static async getStudentFaces(req, res, next) {

        try {

            const result =
                await FaceService.getStudentFaces(

                    req.params.studentId

                );

            return res.json(

                Response.success(

                    result.message,

                    result.data,

                    {

                        total: result.data.length

                    }

                )

            );

        }
        catch (error) {

            next(error);

        }

    }

    // =====================================================
    // Delete Face
    // DELETE /api/faces/:faceId
    // =====================================================

    static async deleteFace(req, res, next) {

        try {

            const result =
                await FaceService.deleteFace(

                    req.params.faceId

                );

            if (!result.success) {

                return res.status(404).json(

                    Response.error(result.message)

                );

            }

            return res.json(

                Response.success(

                    result.message

                )

            );

        }
        catch (error) {

            next(error);

        }

    }

    // =====================================================
    // Set Primary Face
    // PUT /api/faces/primary/:studentId/:faceId
    // =====================================================

    static async setPrimaryFace(req, res, next) {

        try {

            const result =
                await FaceService.setPrimaryFace(

                    req.params.studentId,

                    req.params.faceId

                );

            return res.json(

                Response.success(

                    result.message

                )

            );

        }
        catch (error) {

            next(error);

        }

    }

}

module.exports = FaceController;