package space.nstart.pcp.pcp_satellite_catalog_service.controller

import jakarta.validation.Valid
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.PutMapping
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_satellite_catalog_service.service.SatelliteCatalogService
import space.nstart.pcp.pcp_types_lib.dto.satellite.SatelliteBatchRequestDTO
import space.nstart.pcp.pcp_types_lib.dto.satellite.SatelliteCreateDTO
import space.nstart.pcp.pcp_types_lib.dto.satellite.SatelliteObservationProfileDTO
import space.nstart.pcp.pcp_types_lib.dto.satellite.SatelliteSlotProfileDTO
import space.nstart.pcp.pcp_types_lib.dto.satellite.SatelliteUpdateDTO

@RestController
@RequestMapping("/api/satellites")
class SatelliteCatalogController(
    private val satelliteCatalogService: SatelliteCatalogService
) {

    @GetMapping
    fun all() = satelliteCatalogService.all()

    @GetMapping("/{satellite_id}")
    fun byId(@PathVariable("satellite_id") satelliteId: Long) =
        satelliteCatalogService.byId(satelliteId)

    @GetMapping("/by-norad/{norad_id}")
    fun byNoradId(@PathVariable("norad_id") noradId: Long) =
        satelliteCatalogService.byNoradId(noradId)

    @PostMapping
    fun create(@Valid @RequestBody request: SatelliteCreateDTO) =
        satelliteCatalogService.createSatellite(request)

    @PutMapping("/{satellite_id}")
    fun update(
        @PathVariable("satellite_id") satelliteId: Long,
        @Valid @RequestBody request: SatelliteUpdateDTO
    ) = satelliteCatalogService.updateSatellite(satelliteId, request)

    @DeleteMapping("/{satellite_id}")
    fun delete(@PathVariable("satellite_id") satelliteId: Long): ResponseEntity<Void> {
        satelliteCatalogService.deleteSatellite(satelliteId)
        return ResponseEntity.noContent().build()
    }

    @GetMapping("/{satellite_id}/observation-profile")
    fun observationProfile(@PathVariable("satellite_id") satelliteId: Long) =
        satelliteCatalogService.observationProfile(satelliteId)

    @PostMapping("/{satellite_id}/observation-profile")
    fun createObservationProfile(
        @PathVariable("satellite_id") satelliteId: Long,
        @Valid @RequestBody request: SatelliteObservationProfileDTO
    ) = satelliteCatalogService.createObservationProfile(satelliteId, request)

    @PutMapping("/{satellite_id}/observation-profile")
    fun updateObservationProfile(
        @PathVariable("satellite_id") satelliteId: Long,
        @Valid @RequestBody request: SatelliteObservationProfileDTO
    ) = satelliteCatalogService.updateObservationProfile(satelliteId, request)

    @DeleteMapping("/{satellite_id}/observation-profile")
    fun deleteObservationProfile(@PathVariable("satellite_id") satelliteId: Long): ResponseEntity<Void> {
        satelliteCatalogService.deleteObservationProfile(satelliteId)
        return ResponseEntity.noContent().build()
    }

    @GetMapping("/{satellite_id}/slot-profile")
    fun slotProfile(@PathVariable("satellite_id") satelliteId: Long) =
        satelliteCatalogService.slotProfile(satelliteId)

    @PostMapping("/{satellite_id}/slot-profile")
    fun createSlotProfile(
        @PathVariable("satellite_id") satelliteId: Long,
        @Valid @RequestBody request: SatelliteSlotProfileDTO
    ) = satelliteCatalogService.createSlotProfile(satelliteId, request)

    @PutMapping("/{satellite_id}/slot-profile")
    fun updateSlotProfile(
        @PathVariable("satellite_id") satelliteId: Long,
        @Valid @RequestBody request: SatelliteSlotProfileDTO
    ) = satelliteCatalogService.updateSlotProfile(satelliteId, request)

    @DeleteMapping("/{satellite_id}/slot-profile")
    fun deleteSlotProfile(@PathVariable("satellite_id") satelliteId: Long): ResponseEntity<Void> {
        satelliteCatalogService.deleteSlotProfile(satelliteId)
        return ResponseEntity.noContent().build()
    }

    @PostMapping("/batch")
    fun batch(@Valid @RequestBody request: SatelliteBatchRequestDTO) =
        satelliteCatalogService.batch(request)
}
