module.exports = (sequelize, DataTypes) => {
  const Designation = sequelize.define('Designation', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    designation_code: {
      type: DataTypes.STRING(20),
      allowNull: false
    },
    designation_name: {
      type: DataTypes.STRING(200),
      allowNull: false
    },
    grade: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    level: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'Hierarchy level'
    },
    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: 'designation',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'designation_code']
      },
      {
        fields: ['status']
      },
      {
        fields: ['effective_from', 'effective_to']
      }
    ]
  });

  Designation.associate = function(models) {
    Designation.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    Designation.hasMany(models.Employee, { foreignKey: 'designation_id', as: 'employees' });
  };

  return Designation;
};

