package at.procon.eventhub.tachographfilesession.api;

import at.procon.eventhub.tachographfilesession.dto.CreateTachographCompositeSessionRequest;
import at.procon.eventhub.tachographfilesession.dto.CreateTachographCompositeSessionResponse;
import at.procon.eventhub.tachographfilesession.dto.TachographCompositeDriverEventsResponse;
import at.procon.eventhub.tachographfilesession.dto.TachographCompositeSessionListDriversResponse;
import at.procon.eventhub.tachographfilesession.dto.TachographCompositeSessionSummaryDto;
import at.procon.eventhub.tachographfilesession.model.ResolvedDriverTimeline;
import at.procon.eventhub.tachographfilesession.service.TachographCompositeSessionService;
import jakarta.validation.Valid;
import java.util.UUID;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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/tachograph-composite-sessions")
public class TachographCompositeSessionController {

    private final TachographCompositeSessionService service;

    public TachographCompositeSessionController(TachographCompositeSessionService service) {
        this.service = service;
    }

    @PostMapping
    public ResponseEntity<CreateTachographCompositeSessionResponse> createCompositeSession(
            @Valid @RequestBody CreateTachographCompositeSessionRequest request
    ) {
        return ResponseEntity.status(HttpStatus.CREATED).body(service.createCompositeSession(request));
    }

    @GetMapping("/{compositeSessionId}")
    public ResponseEntity<TachographCompositeSessionSummaryDto> getCompositeSession(@PathVariable UUID compositeSessionId) {
        return ResponseEntity.ok(service.getCompositeSession(compositeSessionId));
    }

    @GetMapping("/{compositeSessionId}/drivers")
    public ResponseEntity<TachographCompositeSessionListDriversResponse> listDrivers(@PathVariable UUID compositeSessionId) {
        return ResponseEntity.ok(service.listDrivers(compositeSessionId));
    }

    @GetMapping("/{compositeSessionId}/drivers/{driverKey}/events")
    public ResponseEntity<TachographCompositeDriverEventsResponse> getMergedDriverEvents(
            @PathVariable UUID compositeSessionId,
            @PathVariable String driverKey
    ) {
        return ResponseEntity.ok(service.getMergedDriverEvents(compositeSessionId, driverKey));
    }

    @GetMapping("/{compositeSessionId}/drivers/{driverKey}/timeline")
    public ResponseEntity<ResolvedDriverTimeline> getMergedDriverTimeline(
            @PathVariable UUID compositeSessionId,
            @PathVariable String driverKey
    ) {
        return ResponseEntity.ok(service.getMergedDriverTimeline(compositeSessionId, driverKey));
    }
}
