module.exports = (sequelize, DataTypes) => {
  const RoutingOperation = sequelize.define('RoutingOperation', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    routing_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Routing',
        key: 'id'
      }
    },
    operation_number: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    operation_code: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Operation code (from Operation Name master)'
    },
    work_center_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'WorkCenter',
        key: 'id'
      }
    },
    machine_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Machine',
        key: 'id'
      }
    },
    operation_description: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    operation_drawing_ref: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Operation-level drawing reference/name'
    },
    operation_materials_json: {
      type: DataTypes.TEXT('long'),
      allowNull: true,
      comment: 'JSON array of material IDs selected for this operation'
    },
    standard_text_key: {
      type: DataTypes.STRING(8),
      allowNull: true
    },
    control_key: {
      type: DataTypes.STRING(4),
      allowNull: true,
      comment: 'Operation control (e.g., PP01 for production operation)'
    },
    setup_time: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
      comment: 'Setup time in minutes'
    },
    machine_time: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      defaultValue: 0.00,
      comment: 'Machine time per unit'
    },
    labor_time: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      defaultValue: 0.00,
      comment: 'Labor time per unit'
    },
    base_quantity: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      defaultValue: 1.000,
      comment: 'Base quantity for time calculation'
    },
    queue_time: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
      comment: 'Queue time in minutes'
    },
    move_time: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
      comment: 'Move time in minutes'
    },
    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: 'routing_operation',
    indexes: [
      {
        unique: true,
        fields: ['routing_id', 'operation_number']
      },
      {
        fields: ['work_center_id']
      },
      {
        fields: ['status']
      }
    ]
  });

  RoutingOperation.associate = function(models) {
    RoutingOperation.belongsTo(models.Routing, { foreignKey: 'routing_id', as: 'routing' });
    RoutingOperation.belongsTo(models.WorkCenter, { foreignKey: 'work_center_id', as: 'workCenter' });
    RoutingOperation.belongsTo(models.Machine, { foreignKey: 'machine_id', as: 'machine' });
  };

  return RoutingOperation;
};

