class RepairInstancesService {

    public OperationStatus rebootInstances(String accountId, RebootInstancesRequest request) {
        Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithLists(request.getEnvironmentCrn(), accountId);
        MDCBuilder.buildMdcContext(stack);
        Map<String, InstanceMetaData> allInstancesByInstanceId = getAllInstancesFromStack(stack);
        Map<String, InstanceStatus> healthMap = request.isForceReboot() ? Collections.emptyMap() : getInstanceHealthMap(accountId, request.getEnvironmentCrn());
        Map<String, InstanceMetaData> instancesToReboot =
                getInstancesToRepair(healthMap, allInstancesByInstanceId, request.getInstanceIds(), request.isForceReboot(), true);

        if (instancesToReboot.keySet().isEmpty()) {
            throw new NotFoundException("No unhealthy instances to reboot.  Maybe use the force option.");
        }


        Operation operation = operationService.startOperation(accountId, OperationType.REBOOT, Set.of(stack.getEnvironmentCrn()), Collections.emptySet());
        if (operation.getStatus() == OperationState.RUNNING) {
            stackUpdater.updateStackStatus(stack.getId(), DetailedStackStatus.REPAIR_REQUESTED, "Reboot requested");
            flowManager.notify(REBOOT_EVENT.event(), new RebootInstanceEvent(REBOOT_EVENT.event(), stack.getId(),
                    instancesToReboot.keySet().stream().collect(Collectors.toList()), operation.getOperationId()));
        }
        return operationToOperationStatusConverter.convert(operation);
    }

}