module.exports = (sequelize, DataTypes) => {
  const ProductionOperator = sequelize.define(
    'ProductionOperator',
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'Company', key: 'id' },
      },
      operator_name: {
        type: DataTypes.STRING(100),
        allowNull: false,
      },
      operation_name_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'OperationName', key: 'id' },
      },
      work_center_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'WorkCenter', key: 'id' },
      },
      designation: {
        type: DataTypes.STRING(100),
        allowNull: true,
      },
      mobile_no: {
        type: DataTypes.STRING(20),
        allowNull: true,
      },
      shift: {
        type: DataTypes.ENUM('SHIFT_A', 'SHIFT_B', 'SHIFT_C', 'GENERAL'),
        allowNull: false,
        defaultValue: 'GENERAL',
      },
      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: 'production_operator',
      indexes: [
        { fields: ['company_id'] },
        { fields: ['operation_name_id'] },
        { fields: ['work_center_id'] },
        { fields: ['status'] },
      ],
    }
  );

  ProductionOperator.associate = function (models) {
    ProductionOperator.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ProductionOperator.belongsTo(models.OperationName, { foreignKey: 'operation_name_id', as: 'operationName' });
    ProductionOperator.belongsTo(models.WorkCenter, { foreignKey: 'work_center_id', as: 'workCenter' });
    ProductionOperator.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    ProductionOperator.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return ProductionOperator;
};

