const sharp = require("sharp");

// =============================================
// Default Image Configuration
// =============================================

const DEFAULT_WIDTH = 512;
const DEFAULT_HEIGHT = 512;
const DEFAULT_QUALITY = 90;

// =============================================
// Save Student Profile Image
// =============================================

async function saveStudentProfile(buffer, outputPath) {

    try {

        await sharp(buffer)

            // Auto Rotate (Phone Images)
            .rotate()

            // Resize
            .resize(DEFAULT_WIDTH, DEFAULT_HEIGHT, {
                fit: "cover",
                position: "center"
            })

            // Remove Metadata
            .withMetadata(false)

            // Convert to JPEG
            .jpeg({

                quality: DEFAULT_QUALITY,

                progressive: true,

                mozjpeg: true

            })

            // Save
            .toFile(outputPath);

        return true;

    }
    catch (error) {

        console.error("Image Save Error:", error);

        throw error;

    }

}

// =============================================
// Save Attendance Image
// =============================================

async function saveAttendanceImage(buffer, outputPath) {

    try {

        await sharp(buffer)

            .rotate()

            .resize(640, 480, {

                fit: "inside"

            })

            .jpeg({

                quality: 80,

                progressive: true,

                mozjpeg: true

            })

            .toFile(outputPath);

        return true;

    }
    catch (error) {

        console.error("Attendance Image Error:", error);

        throw error;

    }

}

// =============================================
// Save Unknown Face Image
// =============================================

async function saveUnknownFace(buffer, outputPath) {

    try {

        await sharp(buffer)

            .rotate()

            .resize(640, 640, {

                fit: "inside"

            })

            .jpeg({

                quality: 85,

                progressive: true,

                mozjpeg: true

            })

            .toFile(outputPath);

        return true;

    }
    catch (error) {

        console.error("Unknown Face Save Error:", error);

        throw error;

    }

}

// =============================================
// Get Image Information
// =============================================

async function getImageInfo(buffer) {

    try {

        const metadata = await sharp(buffer).metadata();

        return {

            width: metadata.width,

            height: metadata.height,

            format: metadata.format,

            size: buffer.length

        };

    }
    catch (error) {

        throw error;

    }

}

// =============================================
// Validate Image
// =============================================

async function validateImage(buffer) {

    try {

        const metadata = await sharp(buffer).metadata();

        if (!metadata.width || !metadata.height) {

            return false;

        }

        return true;

    }
    catch (error) {

        return false;

    }

}

// =============================================
// Export
// =============================================

module.exports = {

    saveStudentProfile,

    saveAttendanceImage,

    saveUnknownFace,

    getImageInfo,

    validateImage

};