'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('tax_master', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      tax_code: {
        type: Sequelize.STRING(20),
        allowNull: false
      },
      tax_name: {
        type: Sequelize.STRING(200),
        allowNull: false
      },
      tax_type: {
        type: Sequelize.ENUM('GST', 'TDS', 'TCS', 'EXCISE', 'CUSTOMS', 'VAT', 'CESS', 'OTHER'),
        allowNull: false
      },
      tax_rate: {
        type: Sequelize.DECIMAL(5, 2),
        allowNull: false,
        defaultValue: 0.00,
        comment: 'Tax rate in percentage'
      },
      cgst_rate: {
        type: Sequelize.DECIMAL(5, 2),
        allowNull: true,
        comment: 'CGST rate for GST type'
      },
      sgst_rate: {
        type: Sequelize.DECIMAL(5, 2),
        allowNull: true,
        comment: 'SGST rate for GST type'
      },
      igst_rate: {
        type: Sequelize.DECIMAL(5, 2),
        allowNull: true,
        comment: 'IGST rate for GST type'
      },
      cess_rate: {
        type: Sequelize.DECIMAL(5, 2),
        allowNull: true,
        comment: 'CESS rate for GST type'
      },
      gl_account_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'gl_account',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'GL account for tax liability'
      },
      input_gl_account_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'gl_account',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'GL account for input tax credit'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      effective_from: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      effective_to: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('tax_master', ['company_id', 'tax_code'], { unique: true });
    await queryInterface.addIndex('tax_master', ['tax_type']);
    await queryInterface.addIndex('tax_master', ['status']);
    await queryInterface.addIndex('tax_master', ['effective_from', 'effective_to']);
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('tax_master');
  }
};

