import express from "express";
import { verifyToken } from "../auth/auth.js";
import { connection } from "../configs/db.js";

const reportRoute = express.Router();


// ======================================
// A. DAILY SALES REPORT
// Filter sales by specific date
// ======================================

reportRoute.get("/daily-sales", verifyToken, (req, res) => {

    const { date } = req.query;

    const sql = `
        SELECT *
        FROM sales
        WHERE DATE(sale_date) = ?
        ORDER BY sale_id DESC
    `;

    (async () => {
        try {
            const [result] = await connection.query(sql, [date]);
            return res.status(200).json({
                success: true,
                total_sales: result.length,
                data: result
            });
        } catch (err) {
            return res.status(500).json({
                success: false,
                message: err.message
            });
        }
    })();
});



// ======================================
// B. MONTHLY SALES REPORT
// Filter by start_date and end_date
// ======================================

reportRoute.get("/monthly-sales", verifyToken, (req, res) => {

    const { start_date, end_date } = req.query;

    const sql = `
        SELECT *
        FROM sales
        WHERE DATE(sale_date)
        BETWEEN ? AND ?
        ORDER BY sale_date DESC
    `;

    (async () => {
        try {
            const [result] = await connection.query(sql, [start_date, end_date]);
            return res.status(200).json({
                success: true,
                total_sales: result.length,
                data: result
            });
        } catch (err) {
            return res.status(500).json({
                success: false,
                message: err.message
            });
        }
    })();
});



// ======================================
// C. PRODUCT STOCK REPORT
// Display available stock and sold stock
// ======================================

reportRoute.get("/product-stock", verifyToken, (req, res) => {

    const sql = `
        SELECT 
            p.product_id,
            p.product_name,
            p.category,
            p.quantity AS available_stock,

            IFNULL(
                SUM(s.quantity),
                0
            ) AS sold_stock

        FROM products p

        LEFT JOIN sales s
        ON p.product_name = s.product_name

        GROUP BY p.product_id
        ORDER BY p.product_id DESC
    `;

    connection.query(sql, (err, result) => {

        if (err) {
            return res.status(500).json({
                success: false,
                message: err.message
            });
        }

        res.status(200).json({
            success: true,
            data: result
        });
    });
});



// ======================================
// D. CUSTOMER PURCHASE REPORT
// Display all purchases made by customer
// ======================================

reportRoute.get("/customer-purchase/:customer_name", verifyToken, (req, res) => {

    const { customer_name } = req.params;

    const sql = `
        SELECT *
        FROM sales
        WHERE customer_name = ?
        ORDER BY sale_date DESC
    `;

    connection.query(
        sql,
        [customer_name],
        (err, result) => {

            if (err) {
                return res.status(500).json({
                    success: false,
                    message: err.message
                });
            }

            res.status(200).json({
                success: true,
                customer_name,
                total_purchases: result.length,
                data: result
            });
        }
    );
});


export default reportRoute;