package at.procon.eventhub.tachographfilesession.api;

import at.procon.eventhub.tachographfilesession.dto.CreateTachographFileSessionResponse;
import at.procon.eventhub.tachographfilesession.dto.TachographEsperEventsProcessingRequest;
import at.procon.eventhub.tachographfilesession.dto.TachographEsperDriverProcessingResultDto;
import at.procon.eventhub.tachographfilesession.dto.TachographFileDriverDetailDto;
import at.procon.eventhub.tachographfilesession.dto.TachographOperatingPeriodsProcessingRequest;
import at.procon.eventhub.tachographfilesession.dto.TachographOperatingPeriodsProcessingResultDto;
import at.procon.eventhub.tachographfilesession.dto.TachographFileSessionDeleteResponse;
import at.procon.eventhub.tachographfilesession.dto.TachographFileSessionListDriversResponse;
import at.procon.eventhub.tachographfilesession.dto.TachographFileSessionSummaryDto;
import at.procon.eventhub.tachographfilesession.service.TachographFileSessionProcessingService;
import at.procon.eventhub.tachographfilesession.service.TachographFileSessionService;
import java.util.UUID;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/eventhub/tachograph-file-sessions")
public class TachographFileSessionController {

    private final TachographFileSessionService service;
    private final TachographFileSessionProcessingService processingService;

    public TachographFileSessionController(
            TachographFileSessionService service,
            TachographFileSessionProcessingService processingService
    ) {
        this.service = service;
        this.processingService = processingService;
    }

    @PostMapping
    public ResponseEntity<CreateTachographFileSessionResponse> createSession(
            @RequestParam("file") MultipartFile file,
            @RequestParam(required = false) String tenantKey,
            @RequestParam(required = false) String sourceInstanceKey,
            @RequestParam(required = false) String sessionLabel
    ) {
        return ResponseEntity.status(HttpStatus.CREATED)
                .body(service.createSession(file, tenantKey, sourceInstanceKey, sessionLabel));
    }

    @GetMapping("/{sessionId}")
    public ResponseEntity<TachographFileSessionSummaryDto> getSession(@PathVariable UUID sessionId) {
        return ResponseEntity.ok(service.getSession(sessionId));
    }

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

    @GetMapping("/{sessionId}/drivers/{driverKey}")
    public ResponseEntity<TachographFileDriverDetailDto> getDriver(
            @PathVariable UUID sessionId,
            @PathVariable String driverKey
    ) {
        return ResponseEntity.ok(service.getDriver(sessionId, driverKey));
    }

    @GetMapping("/{sessionId}/drivers/{driverKey}/processing/esper-events")
    public ResponseEntity<TachographEsperDriverProcessingResultDto> getEsperDriverProcessingResults(
            @PathVariable UUID sessionId,
            @PathVariable String driverKey
    ) {
        return ResponseEntity.ok(processingService.getEsperDriverProcessingResults(sessionId, driverKey));
    }

    @PostMapping("/{sessionId}/drivers/{driverKey}/processing/esper-events")
    public ResponseEntity<TachographEsperDriverProcessingResultDto> evaluateEsperDriverProcessingResults(
            @PathVariable UUID sessionId,
            @PathVariable String driverKey,
            @RequestBody(required = false) TachographEsperEventsProcessingRequest request
    ) {
        return ResponseEntity.ok(processingService.getEsperDriverProcessingResults(sessionId, driverKey, request));
    }

    @PostMapping("/{sessionId}/drivers/{driverKey}/processing/operating-periods")
    public ResponseEntity<TachographOperatingPeriodsProcessingResultDto> evaluateOperatingPeriods(
            @PathVariable UUID sessionId,
            @PathVariable String driverKey,
            @RequestBody(required = false) TachographOperatingPeriodsProcessingRequest request
    ) {
        return ResponseEntity.ok(processingService.evaluateOperatingPeriods(sessionId, driverKey, request));
    }

    @DeleteMapping("/{sessionId}")
    public ResponseEntity<TachographFileSessionDeleteResponse> deleteSession(@PathVariable UUID sessionId) {
        return ResponseEntity.ok(service.deleteSession(sessionId));
    }
}
