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

const customerRoute = express.Router();


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

    const { full_name, phone_number, email, address } = req.body;

    const sql = `
        INSERT INTO customers
        (full_name, phone_number, email, address)
        VALUES (?, ?, ?, ?)
    `;

    try {
        const [result] = await connection.query(sql, [full_name, phone_number, email, address]);
        res.status(201).json({
            success: true,
            message: "Customer added successfully",
            data: result
        });
    } catch (err) {
        res.status(500).json({
            success: false,
            message: err.message
        });
    }
});


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

    const sql = `SELECT * FROM customers ORDER BY customer_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 Customer
customerRoute.get("/:id", verifyToken, async (req, res) => {

    const { id } = req.params;

    const sql = `SELECT * FROM customers WHERE customer_id = ?`;

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

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

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


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

    const { id } = req.params;
    const { full_name, phone_number, email, address } = req.body;

    const sql = `
        UPDATE customers
        SET full_name = ?, phone_number = ?, email = ?, address = ?
        WHERE customer_id = ?
    `;

    try {
        await connection.query(sql, [full_name, phone_number, email, address, id]);
        res.status(200).json({
            success: true,
            message: "Customer updated successfully"
        });
    } catch (err) {
        res.status(500).json({
            success: false,
            message: err.message
        });
    }
});


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

    const { id } = req.params;

    const sql = `DELETE FROM customers WHERE customer_id = ?`;

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


export default customerRoute;