package com.dowstats.controllers

import com.dowstats.data.dto.controllers.building.BuildingFullDto
import com.dowstats.data.dto.controllers.building.RaceBuildings
import com.dowstats.data.repositories.BuildingRepository
import com.dowstats.service.datamaps.DowBuildingMappingService
import org.springframework.beans.factory.annotation.Autowired
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.RequestMapping
import org.springframework.web.bind.annotation.RestController


@RestController
@RequestMapping("api/v1/buildings")
class BuildingsController @Autowired constructor(
    val dowBuildingMappingService: DowBuildingMappingService,
    val buildingRepository: BuildingRepository,
) {

    @GetMapping("/{modId}/{raceId}")
    fun getBuildingsByModAndRace(@PathVariable modId: Long,
                         @PathVariable raceId: String): RaceBuildings {
        return dowBuildingMappingService.findBuildingsByModAndRace(modId, raceId)
    }

    @GetMapping("/mod/{modId}")
    fun getUnitsByMod(@PathVariable modId: Long,): List<RaceBuildings> {
        return dowBuildingMappingService.findBuildingsByMod(modId)
    }


    @GetMapping("/{buildingId}")
    fun getById(@PathVariable buildingId: Long): BuildingFullDto {
        return dowBuildingMappingService.mapToDto(buildingRepository.findById(buildingId).get())
    }

    @DeleteMapping
    fun removeAll() {
        buildingRepository.deleteAll()
    }
}