module.exports = (sequelize, DataTypes) => {
  const OperationCode = sequelize.define(
    'OperationCode',
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'Company', key: 'id' }
      },
      operation_code: {
        type: DataTypes.STRING(50),
        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: 'operation_code',
      indexes: [
        { unique: true, fields: ['company_id', 'operation_code'] },
        { fields: ['company_id'] },
        { fields: ['status'] }
      ]
    }
  );

  OperationCode.associate = function(models) {
    OperationCode.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    OperationCode.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    OperationCode.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return OperationCode;
};
