package com.dowstats.controllers

import com.dowstats.data.dto.controllers.RaceUnits
import com.dowstats.data.dto.controllers.UnitFullDto
import com.dowstats.data.repositories.UnitRepository
import com.dowstats.service.datamaps.DowUnitMappingService
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/units")
class UnitsController @Autowired constructor(
    val dowUnitMappingService: DowUnitMappingService,
    val unitsRepo: UnitRepository,
) {

    @GetMapping("/{modId}/{raceId}")
    fun getUnitsByModAndRace(@PathVariable modId: Long,
                         @PathVariable raceId: String): RaceUnits {
        return dowUnitMappingService.findUnitsByModAndRace(modId, raceId)
    }

    @GetMapping("/mod/{modId}")
    fun getUnitsByMod(@PathVariable modId: Long,): List<RaceUnits> {
        return dowUnitMappingService.findUnitsByMod(modId)
    }


    @GetMapping("/{unitId}")
    fun getById(@PathVariable unitId: Long): UnitFullDto {
        return dowUnitMappingService.getUnitFullDto(unitId)
    }

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