package at.procon.eventhub.processing.api;

import at.procon.eventhub.processing.dto.UnifiedRuntimeProcessingApiRequest;
import at.procon.eventhub.processing.model.UnifiedRuntimeEventBundle;
import at.procon.eventhub.processing.service.UnifiedRuntimeDriverTimelineService;
import at.procon.eventhub.processing.service.UnifiedRuntimeEventAssemblyService;
import at.procon.eventhub.tachographfilesession.model.ResolvedDriverTimeline;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/eventhub/runtime-processing")
public class UnifiedRuntimeProcessingController {

    private final UnifiedRuntimeEventAssemblyService eventAssemblyService;
    private final UnifiedRuntimeDriverTimelineService runtimeDriverTimelineService;

    public UnifiedRuntimeProcessingController(
            UnifiedRuntimeEventAssemblyService eventAssemblyService,
            UnifiedRuntimeDriverTimelineService runtimeDriverTimelineService
    ) {
        this.eventAssemblyService = eventAssemblyService;
        this.runtimeDriverTimelineService = runtimeDriverTimelineService;
    }

    @PostMapping("/driver-events")
    public ResponseEntity<UnifiedRuntimeEventBundle> loadDriverEvents(
            @RequestBody UnifiedRuntimeProcessingApiRequest request
    ) {
        return ResponseEntity.ok(eventAssemblyService.assembleDriverScopedEvents(request.toRuntimeRequest()));
    }

    @PostMapping("/driver-timeline")
    public ResponseEntity<ResolvedDriverTimeline> loadDriverTimeline(
            @RequestBody UnifiedRuntimeProcessingApiRequest request
    ) {
        return ResponseEntity.ok(runtimeDriverTimelineService.loadDriverTimeline(request.toRuntimeRequest()));
    }
}
