module.exports = (sequelize, DataTypes) => {
  const OperationName = sequelize.define(
    'OperationName',
    {
      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
      },
      operation_no: {
        type: DataTypes.INTEGER,
        allowNull: true
      },
      department_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'Department', key: 'id' }
      },
      description: {
        type: DataTypes.STRING(500),
        allowNull: true
      },
      is_dimensional_report_required: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false
      },
      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_name',
      indexes: [
        { unique: true, fields: ['company_id', 'operation_code'] },
        { fields: ['company_id'] },
        { fields: ['department_id'] },
        { fields: ['status'] }
      ]
    }
  );

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

  return OperationName;
};

