'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 IN ('2200', '2210')"),
      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 gstPayableId = glAccounts[0].find(acc => acc.account_code === '2200')?.id || null;
    const gstInputId = glAccounts[0].find(acc => acc.account_code === '2210')?.id || null;

    await queryInterface.bulkInsert('tax_master', [
      {
        company_id: companyId,
        tax_code: 'GST05',
        tax_name: 'GST 5%',
        tax_type: 'GST',
        tax_rate: 5.00,
        cgst_rate: 2.50,
        sgst_rate: 2.50,
        igst_rate: 5.00,
        gl_account_id: gstPayableId,
        input_gl_account_id: gstInputId,
        status: 'ACTIVE',
        effective_from: '2020-01-01',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        tax_code: 'GST12',
        tax_name: 'GST 12%',
        tax_type: 'GST',
        tax_rate: 12.00,
        cgst_rate: 6.00,
        sgst_rate: 6.00,
        igst_rate: 12.00,
        gl_account_id: gstPayableId,
        input_gl_account_id: gstInputId,
        status: 'ACTIVE',
        effective_from: '2020-01-01',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        tax_code: 'GST18',
        tax_name: 'GST 18%',
        tax_type: 'GST',
        tax_rate: 18.00,
        cgst_rate: 9.00,
        sgst_rate: 9.00,
        igst_rate: 18.00,
        gl_account_id: gstPayableId,
        input_gl_account_id: gstInputId,
        status: 'ACTIVE',
        effective_from: '2020-01-01',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        tax_code: 'GST28',
        tax_name: 'GST 28%',
        tax_type: 'GST',
        tax_rate: 28.00,
        cgst_rate: 14.00,
        sgst_rate: 14.00,
        igst_rate: 28.00,
        gl_account_id: gstPayableId,
        input_gl_account_id: gstInputId,
        status: 'ACTIVE',
        effective_from: '2020-01-01',
        created_by: userId,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        company_id: companyId,
        tax_code: 'GST00',
        tax_name: 'GST 0% (Exempt)',
        tax_type: 'GST',
        tax_rate: 0.00,
        cgst_rate: 0.00,
        sgst_rate: 0.00,
        igst_rate: 0.00,
        gl_account_id: gstPayableId,
        input_gl_account_id: gstInputId,
        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('tax_master', null, {});
  }
};

