// controllers/borrowTransactionController.js

const BorrowTransaction = require('../models/BorrowTransaction');

/**
 * สร้างรายการยืมอุปกรณ์ (Create)
 */
exports.createBorrowTransaction = async (req, res) => {
  try {
    // ข้อมูลจาก body เช่น: { user_id, project_id, equipment_id, quantity_borrowed, ... }
    const {
      user_id,
      project_id,
      equipment_id,
      quantity_borrowed,
      borrowed_date,
      due_date,
      status,
    } = req.body;

    const newTransaction = await BorrowTransaction.create({
      user_id,
      project_id,
      equipment_id,
      quantity_borrowed,
      borrowed_date,
      due_date,
      status,
    });

    return res.status(201).json({
      message: 'BorrowTransaction created successfully',
      borrowTransaction: newTransaction
    });
  } catch (error) {
    console.error(error);
    return res.status(500).json({ error: error.message });
  }
};

/**
 * ดึงข้อมูลการยืมทั้งหมด (Read All)
 */
exports.getAllBorrowTransactions = async (req, res) => {
  try {
    const transactions = await BorrowTransaction.findAll();
    return res.status(200).json(transactions);
  } catch (error) {
    console.error(error);
    return res.status(500).json({ error: error.message });
  }
};

/**
 * ดึงข้อมูลการยืมตาม borrow_id (Read One)
 */
exports.getBorrowTransactionById = async (req, res) => {
  try {
    const { id } = req.params; // borrow_id
    const transaction = await BorrowTransaction.findByPk(id);

    if (!transaction) {
      return res.status(404).json({ error: 'BorrowTransaction not found' });
    }
    return res.status(200).json(transaction);
  } catch (error) {
    console.error(error);
    return res.status(500).json({ error: error.message });
  }
};

/**
 * อัปเดตข้อมูลการยืม (Update)
 */
exports.updateBorrowTransaction = async (req, res) => {
  try {
    const { id } = req.params;
    const {
      user_id,
      project_id,
      equipment_id,
      quantity_borrowed,
      borrowed_date,
      due_date,
      returned_date,
      status,
    } = req.body;

    const [rowsUpdated] = await BorrowTransaction.update({
      user_id,
      project_id,
      equipment_id,
      quantity_borrowed,
      borrowed_date,
      due_date,
      returned_date,
      status
    }, {
      where: { borrow_id: id },
    });

    if (!rowsUpdated) {
      return res.status(404).json({ error: 'BorrowTransaction not found or no changes' });
    }

    // ดึงข้อมูลใหม่ที่เพิ่งอัปเดต
    const updatedTransaction = await BorrowTransaction.findByPk(id);
    return res.status(200).json({
      message: 'BorrowTransaction updated successfully',
      borrowTransaction: updatedTransaction,
    });
  } catch (error) {
    console.error(error);
    return res.status(500).json({ error: error.message });
  }
};

/**
 * ลบข้อมูลการยืม (Delete)
 */
exports.deleteBorrowTransaction = async (req, res) => {
  try {
    const { id } = req.params;
    const rowsDeleted = await BorrowTransaction.destroy({ where: { borrow_id: id } });

    if (!rowsDeleted) {
      return res.status(404).json({ error: 'BorrowTransaction not found' });
    }

    return res.status(200).json({ message: 'BorrowTransaction deleted successfully' });
  } catch (error) {
    console.error(error);
    return res.status(500).json({ error: error.message });
  }
};
