package space.nstart.pcp.slots_service.controller

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 space.nstart.pcp.pcp_types_lib.dto.satellite.mission.MissionRequestDTO
import space.nstart.pcp.slots_service.service.MissionService
import space.nstart.pcp.slots_service.service.SatelliteCatalogService
import space.nstart.pcp.slots_service.service.SlotService
import java.util.UUID

@RestController
@RequestMapping("/api/current-plans")
class CurrentPlansController(
    private val satelliteCatalogService: SatelliteCatalogService,
    private val missionService: MissionService,
    private val slotService: SlotService
) {

    @GetMapping("/satellites")
    fun satellites() = satelliteCatalogService.allSatellites()

    @GetMapping("/satellites/{satelliteId}/missions")
    fun missions(
        @PathVariable satelliteId: Long,
        @RequestParam(defaultValue = "0") page: Int,
        @RequestParam(defaultValue = "4") size: Int
    ) = missionService.missionsBySatellite(satelliteId, page, size)

    @PostMapping("/missions")
    fun createMission(@Valid @RequestBody body: MissionRequestDTO) = missionService.createMission(body)

    @GetMapping("/missions/{missionId}/modes")
    fun modes(@PathVariable missionId: UUID) = missionService.modesByMission(missionId)

    @GetMapping("/booked-slots")
    fun bookedSlots(@RequestParam ids: List<Long>) = slotService.bookedByIds(ids)

    @PostMapping("/missions/{missionId}/surveys/calculate")
    fun calculateSurveys(
        @PathVariable missionId: UUID,
        @RequestParam(name = "comPlanSnapshotId", required = false) comPlanSnapshotId: Long?
    ) = missionService.calculateSurveys(missionId, comPlanSnapshotId)

    @PostMapping("/missions/{missionId}/drops/calculate")
    fun calculateDrops(@PathVariable missionId: UUID) = missionService.calculateDrops(missionId)

    @PostMapping("/missions/{missionId}/confirm")
    fun confirmMission(@PathVariable missionId: UUID) = missionService.confirmMission(missionId)
}
