'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const [companies, glAccounts, users] = await Promise.all([
      queryInterface.sequelize.query("SELECT id FROM company WHERE code = 'COMP001' LIMIT 1"),
      queryInterface.sequelize.query("SELECT id, account_code FROM gl_account WHERE account_code LIKE '22%' LIMIT 1"),
      queryInterface.sequelize.query("SELECT id FROM user WHERE username = 'admin' LIMIT 1")
    ]);

    if (companies[0].length === 0 || users[0].length === 0) return;

    const companyId = companies[0][0].id;
    const userId = users[0][0].id;
    const glAccountId = glAccounts[0].length > 0 ? glAccounts[0][0].id : null;

    await queryInterface.bulkInsert('tds_master', [
      {
        company_id: companyId,
        section_code: '194A',
        section_description: 'Interest other than interest on securities',
        nature_of_payment: 'Interest',
        tds_rate: 10.00,
        threshold_limit: 40000.00,
        gl_account_id: glAccountId,
        status: 'ACTIVE',
        effective_from: '2020-01-01',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        section_code: '194C',
        section_description: 'Payment to contractors',
        nature_of_payment: 'Contractor Payment',
        tds_rate: 2.00,
        threshold_limit: 30000.00,
        gl_account_id: glAccountId,
        status: 'ACTIVE',
        effective_from: '2020-01-01',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        section_code: '194H',
        section_description: 'Commission or brokerage',
        nature_of_payment: 'Commission/Brokerage',
        tds_rate: 5.00,
        threshold_limit: 15000.00,
        gl_account_id: glAccountId,
        status: 'ACTIVE',
        effective_from: '2020-01-01',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        section_code: '194I',
        section_description: 'Rent',
        nature_of_payment: 'Rent',
        tds_rate: 10.00,
        threshold_limit: 240000.00,
        gl_account_id: glAccountId,
        status: 'ACTIVE',
        effective_from: '2020-01-01',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        section_code: '194J',
        section_description: 'Fees for professional or technical services',
        nature_of_payment: 'Professional Fees',
        tds_rate: 10.00,
        threshold_limit: 30000.00,
        gl_account_id: glAccountId,
        status: 'ACTIVE',
        effective_from: '2020-01-01',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      }
    ]);
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.bulkDelete('tds_master', null, {});
  }
};

