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

const salesRoute = express.Router();


// Add Sale
salesRoute.post("/add", verifyToken, async (req, res) => {

    const {
        customer_name,
        product_name,
        quantity,
        unit_price,
        total_price
    } = req.body;

    const sql = `
        INSERT INTO sales
        (customer_name, product_name, quantity, unit_price, total_price)
        VALUES (?, ?, ?, ?, ?)
    `;

    try {
        const [result] = await connection.query(sql, [
            customer_name,
            product_name,
            quantity,
            unit_price,
            total_price
        ]);
        res.status(201).json({
            success: true,
            message: "Sale added successfully",
            data: result
        });
    } catch (err) {
        res.status(500).json({
            success: false,
            message: err.message
        });
    }
});


// Get All Sales
salesRoute.get("/", verifyToken, async (req, res) => {

    const sql = `SELECT * FROM sales ORDER BY sale_id DESC`;

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


// Get Single Sale
salesRoute.get("/:id", verifyToken, async (req, res) => {

    const { id } = req.params;

    const sql = `SELECT * FROM sales WHERE sale_id = ?`;

    try {
        const [result] = await connection.query(sql, [id]);

        if (result.length === 0) {
            return res.status(404).json({
                success: false,
                message: "Sale not found"
            });
        }

        res.status(200).json({
            success: true,
            data: result[0]
        });
    } catch (err) {
        res.status(500).json({
            success: false,
            message: err.message
        });
    }
});


// Update Sale
salesRoute.put("/update/:id", verifyToken, async (req, res) => {

    const { id } = req.params;

    const {
        customer_name,
        product_name,
        quantity,
        unit_price,
        total_price
    } = req.body;

    const sql = `
        UPDATE sales
        SET customer_name = ?,
            product_name = ?,
            quantity = ?,
            unit_price = ?,
            total_price = ?
        WHERE sale_id = ?
    `;

    try {
        await connection.query(sql, [
            customer_name,
            product_name,
            quantity,
            unit_price,
            total_price,
            id
        ]);
        res.status(200).json({
            success: true,
            message: "Sale updated successfully"
        });
    } catch (err) {
        res.status(500).json({
            success: false,
            message: err.message
        });
    }
});


// Delete Sale
salesRoute.delete("/delete/:id", verifyToken, async (req, res) => {

    const { id } = req.params;

    const sql = `DELETE FROM sales WHERE sale_id = ?`;

    try {
        await connection.query(sql, [id]);
        res.status(200).json({
            success: true,
            message: "Sale deleted successfully"
        });
    } catch (err) {
        res.status(500).json({
            success: false,
            message: err.message
        });
    }
});


export default salesRoute;