/**
 * ==================================================
 * Dashboard Controller
 * ==================================================
 */

const DashboardService = require("../services/dashboard.service");
const SystemHealthService = require("../services/systemHealth.service");
const AnalyticsService = require("../services/analytics.service");

// =====================================
// Dashboard Statistics
// =====================================

exports.getStatistics = async (req, res) => {

    try {

        const statistics =
            await DashboardService.getStatistics();

        res.json({

            success: true,

            statistics

        });

    }

    catch (error) {

        console.error(error);

        res.status(500).json({

            success: false,

            message: error.message

        });

    }

};

// =====================================
// System Health
// =====================================

exports.systemHealth = async (req, res) => {

    try {

        const health =
            await SystemHealthService.getHealth();

        res.json({

            success: true,

            health

        });

    }

    catch (error) {

        console.error(error);

        res.status(500).json({

            success: false,

            message: error.message

        });

    }

};

// =====================================
// Attendance Analytics
// =====================================

exports.getAttendanceAnalytics = async (req, res) => {

    try {

        const analytics =
            await AnalyticsService.getAttendanceAnalytics();

        res.json({

            success: true,

            analytics

        });

    }

    catch (error) {

        console.error(error);

        res.status(500).json({

            success: false,

            message: error.message

        });

    }

};

// =====================================
// Recent Attendance
// =====================================

exports.getRecentAttendance = async (req, res) => {

    try {

        const limit = parseInt(req.query.limit) || 10;

        const recentAttendance =
            await DashboardService.getRecentAttendance(limit);

        res.json({

            success: true,

            data: recentAttendance

        });

    }

    catch (error) {

        console.error(error);

        res.status(500).json({

            success: false,

            message: error.message

        });

    }

};