const Borrowtransaction = require('../models/borrowtransaction');
exports.createBorrowTransaction = async (req, res) => {
    try {
        const { borrow_id,member_id, product_id, borrow_date,borrow_name, borrow_return, borrow_status, borrow_remark } = req.body;
        const newTransaction = await Borrowtransaction.create({
            borrow_id,
            member_id,
            product_id,
            borrow_name,
            borrow_date,
            borrow_return,
            borrow_status,
            borrow_remark,
        });
        res.status(201).json({ actionStatus: 'Success', message: 'Borrow Transaction Created!', newTransaction });
    } catch (err) {
        res.status(400).json({ error: err.message });
    }
};

exports.getBorrowTransactions = async (req, res) => {
    try {
        const transactions = await Borrowtransaction.findAll();
        res.status(200).json(transactions);
    } catch (err) {
        res.status(400).json({ error: err.message });
    }
};

exports.getBorrowTransactionById = async (req, res) => {
    try {
        const { id } = req.params;
        const transaction = await Borrowtransaction.findByPk(id);
        if (!transaction) {
            return res.status(404).json({ message: 'Transaction not found' });
        }
        res.status(200).json(transaction);
    } catch (err) {
        res.status(400).json({ error: err.message });
    }
};

exports.updateBorrowTransaction = async (req, res) => {
    try {
        const { id } = req.params;
        const { borrow_id,member_id, product_id, borrow_date,borrow_name, borrow_return, borrow_status, borrow_remark  } = req.body;
        const transaction = await Borrowtransaction.findByPk(id);
        if (!transaction) {
            return res.status(404).json({ message: 'Transaction not found' });
        }
        await transaction.update({ borrow_id,
            member_id,
            product_id,
            borrow_name,
            borrow_date,
            borrow_return,
            borrow_status,
            borrow_remark, });
            
        res.status(200).json({ message: 'Transaction updated successfully', transaction });
    } catch (err) {
        res.status(400).json({ error: err.message });
    }
};

exports.deleteBorrowTransaction = async (req, res) => {
    try {
        const { id } = req.params;
        const transaction = await Borrowtransaction.findByPk(id);
        if (!transaction) {
            return res.status(404).json({ message: 'Transaction not found' });
        }
        await transaction.destroy();
        res.status(200).json({ message: 'Transaction deleted successfully' });
    } catch (err) {
        res.status(400).json({ error: err.message });
    }
};