module.exports = (sequelize, DataTypes) => {
  const PaymentMode = sequelize.define('PaymentMode', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    mode_code: {
      type: DataTypes.STRING(30),
      allowNull: false
    },
    mode_name: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    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: 'payment_mode_master',
    indexes: [
      { unique: true, fields: ['company_id', 'mode_code'] },
      { fields: ['status'] }
    ]
  });

  PaymentMode.associate = function(models) {
    PaymentMode.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    PaymentMode.belongsTo(models.User, { foreignKey: 'created_by', as: 'creator' });
    PaymentMode.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updater' });
  };

  return PaymentMode;
};
