module.exports = (sequelize, DataTypes) => {
  const TDSMaster = sequelize.define('TDSMaster', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    section_code: {
      type: DataTypes.STRING(20),
      allowNull: false,
      comment: 'TDS Section code (e.g., 194A, 194H)'
    },
    section_description: {
      type: DataTypes.STRING(500),
      allowNull: false
    },
    nature_of_payment: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    tds_rate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0.00,
      comment: 'TDS rate in percentage'
    },
    threshold_limit: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      comment: 'Threshold limit for TDS deduction'
    },
    gl_account_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'GLAccount',
        key: 'id'
      },
      comment: 'GL account for TDS payable'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    effective_from: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    effective_to: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'tds_master',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'section_code']
      },
      {
        fields: ['status']
      },
      {
        fields: ['effective_from', 'effective_to']
      }
    ]
  });

  TDSMaster.associate = function(models) {
    TDSMaster.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    TDSMaster.belongsTo(models.GLAccount, { foreignKey: 'gl_account_id', as: 'glAccount' });
  };

  return TDSMaster;
};

