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

class AnalyticsService {

    async getAttendanceAnalytics(days = 7) {

        const [rows] = await db.query(

            `
            SELECT

                attendance_date,

                COUNT(*) AS total

            FROM attendance

            WHERE attendance_date >= DATE_SUB(CURDATE(), INTERVAL ? DAY)

            GROUP BY attendance_date

            ORDER BY attendance_date ASC
            `,

            [days]

        );

        const labels = [];
        const attendance = [];

        rows.forEach((row) => {

            const date = new Date(row.attendance_date);

            labels.push(

                date.toLocaleDateString(

                    "en-US",

                    {

                        month: "short",

                        day: "2-digit"

                    }

                )

            );

            attendance.push(

                Number(row.total)

            );

        });

        return {

            labels,

            attendance

        };

    }

}

module.exports = new AnalyticsService();