
const Company = require('../models/Company'); 
const Branch = require('../models/Branch');
const Item = require('../models/Item');
const BranchInventory = require('./models/BranchInventory');

const Transaction = require('../models/Transaction');
const TransactionDetail = require('./models/TransactionDetail');
const Customer = require('../models/Customer');  // Added Customer model
const DailySales = require('../models/DailySales');  // Added DailySales model
const Transfer = require('../models/Transfer');  // Added Transfer model

async function deleteCompanyRecords(companyId) {
  try {
    const objectId = mongoose.Types.ObjectId(companyId); // Convert to ObjectId if needed

    // Delete from Company collection
    // await Company.deleteMany({ _id: objectId });
    // console.log('Deleted companies with companyId:', companyId);

    // Delete from Branch collection
    // await Branch.deleteMany({ companyId: objectId });
    // console.log('Deleted branches with companyId:', companyId);

    // Delete from Item collection
    await Item.deleteMany({ companyId: objectId });
    console.log('Deleted items with companyId:', companyId);

    // Delete from BranchInventory collection
    await BranchInventory.deleteMany({ companyId: objectId });
    console.log('Deleted branch inventories with companyId:', companyId);

    // // Delete from User collection
    // await User.deleteMany({ companyId: objectId });
    // console.log('Deleted users with companyId:', companyId);

    // // Delete from Role collection
    // await Role.deleteMany({ companyId: objectId });
    // console.log('Deleted roles with companyId:', companyId);

    // // Delete from Permission collection
    // await Permission.deleteMany({ companyId: objectId });
    // console.log('Deleted permissions with companyId:', companyId);

    // Delete from Transaction collection
    await Transaction.deleteMany({ companyId: objectId });
    console.log('Deleted transactions with companyId:', companyId);

    // Delete from TransactionDetail collection
    await TransactionDetail.deleteMany({ companyId: objectId });
    console.log('Deleted transaction details with companyId:', companyId);

    // Delete from Customer collection
    await Customer.deleteMany({ companyId: objectId });
    console.log('Deleted customers with companyId:', companyId);

    // Delete from DailySales collection
    await DailySales.deleteMany({ companyId: objectId });
    console.log('Deleted daily sales with companyId:', companyId);

    // Delete from Transfer collection
    await Transfer.deleteMany({ companyId: objectId });
    console.log('Deleted transfers with companyId:', companyId);

    // Add more collections as needed
    console.log('All records with companyId deleted from relevant collections.');
  } catch (error) {
    console.error('Error deleting company records:', error);
  }
}

// Example call to the function
deleteCompanyRecords('your-company-id-here'); // Pass the companyId to delete
