const BankAccount = require('../models/BankAccount');
const mongoose = require('mongoose');

// Create a new bank account
exports.createBankAccount = async (req, res) => {
  try {
    const { accountNumber, bankName, balance, accountHolderName, branchId, companyId } = req.body;

  

    // Create a new BankAccount instance
    const newBankAccount = new BankAccount({
      accountNumber,
      bankName,
      balance,
      accountHolderName,
      branchId,
      companyId
    });

    // Log the bank account before saving
    console.log("New Bank Account:", newBankAccount);

    // Save the new bank account
    const savedAccount = await newBankAccount.save();
    console.log("Saved Bank Account:", savedAccount);

    // Respond with the saved account
    res.status(201).json(savedAccount);
  } catch (error) {
    console.error("Error creating bank account:", error);
    res.status(500).json({ message: 'Error creating bank account', error: error.message });
  }
};

// Get all bank accounts
exports.getAllBankAccounts = async (req, res) => {
  try {
    const companyId=req.query.companyId;
    const bankAccounts = await BankAccount.find({companyId:companyId}).populate('branchId').populate('companyId');
    res.status(200).json(bankAccounts);
  } catch (error) {
    console.log("error is", error.message);
    res.status(500).json({ message: 'Error fetching bank accounts', error });
  }
};

// Get a single bank account by ID
exports.getBankAccountById = async (req, res) => {
  try {
    const bankAccount = await BankAccount.findById(req.params.id).populate('branchId').populate('companyId');
    if (!bankAccount) {
      return res.status(404).json({ message: 'Bank account not found' });
    }
    res.status(200).json(bankAccount);
  } catch (error) {
    res.status(500).json({ message: 'Error fetching bank account', error });
  }
};

// Update a bank account by ID
exports.updateBankAccount = async (req, res) => {
  try {
    const { accountNumber, bankName, balance, accountHolderName, branchId, companyId } = req.body;

    const updatedBankAccount = await BankAccount.findByIdAndUpdate(
      req.params.id,
      {
        accountNumber,
        bankName,
        balance,
        accountHolderName,
        branchId,
        companyId
      },
      { new: true }
    );

    if (!updatedBankAccount) {
      return res.status(404).json({ message: 'Bank account not found' });
    }

    res.status(200).json(updatedBankAccount);
  } catch (error) {
    res.status(500).json({ message: 'Error updating bank account', error });
  }
};

// Delete a bank account by ID
exports.deleteBankAccount = async (req, res) => {
  try {
    const deletedBankAccount = await BankAccount.findByIdAndDelete(req.params.id);
    if (!deletedBankAccount) {
      return res.status(404).json({ message: 'Bank account not found' });
    }
    res.status(200).json({ message: 'Bank account deleted successfully' });
  } catch (error) {
    res.status(500).json({ message: 'Error deleting bank account', error });
  }
};
