package space.nstart.pcp.slots_service.controller

import jakarta.validation.Valid
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller
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.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import space.nstart.pcp.pcp_types_lib.dto.ballistics.StationDTO
import space.nstart.pcp.slots_service.configuration.CustomErrorException
import space.nstart.pcp.slots_service.service.StationService

@Controller
class StationCatalogPageController {

    @GetMapping("/stations")
    fun stationsPage(): String = "stations"
}

@RestController
@RequestMapping("/api/stations")
class StationCatalogRestController(
    private val stationService: StationService
) {

    @GetMapping
    fun allStations() = stationService.allStations()

    @GetMapping("/{stationId}")
    fun station(@PathVariable stationId: String) =
        stationService.station(stationId).block()
            ?: throw CustomErrorException("Не удалось получить станцию $stationId: пустой ответ")

    @PostMapping
    fun saveStation(@Valid @RequestBody request: StationDTO) = stationService.saveStation(request)

    @DeleteMapping("/{stationId}")
    fun deleteStation(@PathVariable stationId: String): ResponseEntity<Void> {
        stationService.deleteStation(stationId)
        return ResponseEntity.noContent().build()
    }
}
