const Product = require('../models/product'); // เรียกใช้งาน Model ของ Product


exports.getAllProducts = async (req, res) => {
    try {
        const product = await Product.findAll();
        res.status(200).json({ actionStatus: 'Success', data: product });
    } catch (error) {
        res.status(500).json({ actionStatus: 'Failed', message: error.message });
    }
};


exports.getProductById = async (req, res) => {
    try {
        const { id } = req.params;
        const product = await Product.findByPk(id);
        if (product) {
            res.status(200).json({ actionStatus: 'Success', data: product });
        } else {
            res.status(404).json({ actionStatus: 'Failed', message: 'Product not found' });
        }
    } catch (error) {
        res.status(500).json({ actionStatus: 'Failed', message: error.message });
    }
};


exports.createProduct = async (req, res) => {
    try {
        const { product_id,product_name, unit_id, product_amount,product_balance ,status,time_date,id_product ,product_remark,category} = req.body;
        const newProduct = await Product.create({ product_id,product_name,unit_id, product_amount,product_balance ,status,time_date,id_product,product_remark,category});
        res.status(201).json({ actionStatus: 'Success', message: 'Product created', data: newProduct });
    } catch (error) {
        res.status(400).json({ actionStatus: 'Failed', message: error.message });
    }
};


exports.updateProduct = async (req, res) => {
    try {
        const { id } = req.params;
        const { product_id,product_name, unit_id, product_amount,product_balance ,status,time_date,id_product ,product_remark,category  } = req.body;
        const product = await Product.findByPk(id);
        if (product) {
            product.id_product =id_product ?? id_product.id_product;
            product.product_id = product_id ?? product.product_id;
            product.product_name = product_name ?? product.product_name;
            product.unit_id = unit_id ?? product.unit_id;
            product.product_amount = product_amount ?? product.product_amount;
            product.product_balance = product_balance ?? product.product_balance;
            product.status = status ?? product.status;
            product.time_date = time_date ?? product.time_date;
            product.product_remark = product_remark ?? product.product_remark;
            product.category = category ?? product.category;

            await product.save();
            res.status(200).json({ actionStatus: 'Success', message: 'Product updated', data: product });
        } else {
            res.status(404).json({ actionStatus: 'Failed', message: 'Product not found' });
        }
    } catch (error) {
        res.status(500).json({ actionStatus: 'Failed', message: error.message });
    }
};


exports.deleteProduct = async (req, res) => {
    try {
        const { id } = req.params;
        const product = await Product.findByPk(id);
        if (product) {
            await product.destroy();
            res.status(200).json({ actionStatus: 'Success', message: 'Product deleted' });
        } else {
            res.status(404).json({ actionStatus: 'Failed', message: 'Product not found' });
        }
    } catch (error) {
        res.status(500).json({ actionStatus: 'Failed', message: error.message });
    }
};
