module.exports = (sequelize, DataTypes) => {
  const WorkCenter = sequelize.define('WorkCenter', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    work_center_code: {
      type: DataTypes.STRING(20),
      allowNull: false
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: false
    },
    work_center_category: {
      type: DataTypes.ENUM('MACHINE', 'LABOR', 'PRODUCTION_LINE', 'ASSEMBLY_LINE', 'OTHER'),
      allowNull: false,
      defaultValue: 'MACHINE'
    },
    cost_center_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CostCenter',
        key: 'id'
      }
    },
    capacity_category: {
      type: DataTypes.STRING(2),
      allowNull: true,
      comment: 'Capacity category code'
    },
    number_of_machines: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 1
    },
    number_of_persons: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 1
    },
    capacity: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      defaultValue: 0.00,
      comment: 'Capacity per day in hours'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE', 'MAINTENANCE'),
      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: 'work_center',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'plant_id', 'work_center_code']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['effective_from', 'effective_to']
      }
    ]
  });

  WorkCenter.associate = function(models) {
    WorkCenter.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    WorkCenter.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    WorkCenter.belongsTo(models.CostCenter, { foreignKey: 'cost_center_id', as: 'costCenter' });
    WorkCenter.hasMany(models.RoutingOperation, { foreignKey: 'work_center_id', as: 'operations' });
  };

  return WorkCenter;
};

