package com.ai.trackwalker.uavaccess.api;

import com.ai.trackwalker.uavaccess.common.AccessApiResponse;
import com.ai.trackwalker.uavaccess.flighthub.ConnectionHealthService;
import com.ai.trackwalker.uavaccess.flighthub.DeviceTopologyService;
import com.ai.trackwalker.uavaccess.mission.MissionCommandRequest;
import com.ai.trackwalker.uavaccess.mission.MissionDispatchRequest;
import com.ai.trackwalker.uavaccess.mission.MissionService;
import com.ai.trackwalker.uavaccess.wayline.WaylineService;
import com.ai.trackwalker.uavaccess.wayline.WaylineUploadRequest;
import jakarta.validation.Valid;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/v1")
public class UavAccessController {
    private final ConnectionHealthService health;
    private final DeviceTopologyService devices;
    private final WaylineService waylines;
    private final MissionService missions;

    public UavAccessController(
            ConnectionHealthService health,
            DeviceTopologyService devices,
            WaylineService waylines,
            MissionService missions
    ) {
        this.health = health;
        this.devices = devices;
        this.waylines = waylines;
        this.missions = missions;
    }

    @GetMapping("/connection/health")
    public AccessApiResponse<?> health(@RequestParam(name = "connection_id", required = false) String connectionId) {
        return AccessApiResponse.ok(health.check(connectionId));
    }

    @GetMapping("/devices")
    public AccessApiResponse<?> devices(@RequestParam(name = "connection_id", required = false) String connectionId) {
        return AccessApiResponse.ok(Map.of("devices", devices.synchronize(connectionId)));
    }

    @PostMapping("/waylines")
    public AccessApiResponse<?> uploadWayline(@Valid @RequestBody WaylineUploadRequest request) {
        return AccessApiResponse.ok(waylines.upload(request));
    }

    @GetMapping("/waylines/{routeVersionId}")
    public AccessApiResponse<?> wayline(
            @PathVariable String routeVersionId,
            @RequestParam("connection_id") String connectionId,
            @RequestParam(name = "refresh", defaultValue = "false") boolean refresh
    ) {
        return AccessApiResponse.ok(refresh
                ? waylines.refreshByRouteVersion(connectionId, routeVersionId)
                : waylines.binding(connectionId, routeVersionId));
    }

    @PostMapping("/missions")
    public AccessApiResponse<?> dispatch(@Valid @RequestBody MissionDispatchRequest request) {
        return AccessApiResponse.ok(missions.dispatch(request));
    }

    @GetMapping("/missions/{platformMissionId}")
    public AccessApiResponse<?> mission(@PathVariable String platformMissionId) {
        return AccessApiResponse.ok(missions.binding(platformMissionId));
    }

    @PostMapping("/missions/{platformMissionId}/commands")
    public AccessApiResponse<?> command(
            @PathVariable String platformMissionId,
            @Valid @RequestBody MissionCommandRequest request
    ) {
        return AccessApiResponse.ok(missions.command(platformMissionId, request));
    }
}
