module.exports = (sequelize, DataTypes) => {
  const ProductionOrderRoutingExecution = sequelize.define('ProductionOrderRoutingExecution', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    production_order_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'ProductionOrder',
        key: 'id'
      }
    },
    routing_operation_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'RoutingOperation',
        key: 'id'
      }
    },
    operation_number: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: 'Operation number from routing'
    },
    work_center_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'WorkCenter',
        key: 'id'
      }
    },
    planned_start_date: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: 'Planned start date/time for this operation'
    },
    planned_end_date: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: 'Planned end date/time for this operation'
    },
    actual_start_date: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: 'Actual start date/time for this operation'
    },
    actual_end_date: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: 'Actual end date/time for this operation'
    },
    planned_setup_time: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: 'Planned setup time in minutes'
    },
    planned_machine_time: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      defaultValue: 0.00,
      comment: 'Planned machine time per unit'
    },
    planned_labor_time: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      defaultValue: 0.00,
      comment: 'Planned labor time per unit'
    },
    actual_setup_time: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'Actual setup time in minutes'
    },
    actual_machine_time: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      comment: 'Actual machine time per unit'
    },
    actual_labor_time: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      comment: 'Actual labor time per unit'
    },
    confirmed_quantity: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: false,
      defaultValue: 0.000,
      comment: 'Confirmed quantity for this operation'
    },
    status: {
      type: DataTypes.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'SKIPPED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'PENDING',
      comment: 'Operation execution status'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Remarks or notes for this operation'
    },
    started_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      },
      comment: 'User who started this operation'
    },
    completed_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      },
      comment: 'User who completed this operation'
    },
    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_order_routing_execution',
    indexes: [
      {
        fields: ['production_order_id']
      },
      {
        fields: ['routing_operation_id']
      },
      {
        fields: ['work_center_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['planned_start_date', 'planned_end_date']
      },
      {
        unique: true,
        fields: ['production_order_id', 'operation_number']
      }
    ]
  });

  ProductionOrderRoutingExecution.associate = function(models) {
    ProductionOrderRoutingExecution.belongsTo(models.ProductionOrder, {
      foreignKey: 'production_order_id',
      as: 'productionOrder'
    });
    ProductionOrderRoutingExecution.belongsTo(models.RoutingOperation, {
      foreignKey: 'routing_operation_id',
      as: 'routingOperation'
    });
    ProductionOrderRoutingExecution.belongsTo(models.WorkCenter, {
      foreignKey: 'work_center_id',
      as: 'workCenter'
    });
    ProductionOrderRoutingExecution.belongsTo(models.User, {
      foreignKey: 'started_by',
      as: 'startedByUser'
    });
    ProductionOrderRoutingExecution.belongsTo(models.User, {
      foreignKey: 'completed_by',
      as: 'completedByUser'
    });
    ProductionOrderRoutingExecution.belongsTo(models.User, {
      foreignKey: 'created_by',
      as: 'createdByUser'
    });
    ProductionOrderRoutingExecution.belongsTo(models.User, {
      foreignKey: 'updated_by',
      as: 'updatedByUser'
    });
  };

  return ProductionOrderRoutingExecution;
};

