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

const productRoute = express.Router();


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

    const { product_name, category, quantity, price } = req.body;

    const sql = `
        INSERT INTO products
        (product_name, category, quantity, price)
        VALUES (?, ?, ?, ?)
    `;

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


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

    const sql = `SELECT * FROM products ORDER BY product_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 Product
productRoute.get("/:id", verifyToken, async (req, res) => {

    const { id } = req.params;

    const sql = `SELECT * FROM products WHERE product_id = ?`;

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

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

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


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

    const { id } = req.params;
    const { product_name, category, quantity, price } = req.body;

    const sql = `
        UPDATE products
        SET product_name = ?, category = ?, quantity = ?, price = ?
        WHERE product_id = ?
    `;

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


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

    const { id } = req.params;

    const sql = `DELETE FROM products WHERE product_id = ?`;

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


export default productRoute;