package com.dowstats.data.repositories

import com.dowstats.data.entities.unit.DowUnit
import com.dowstats.data.entities.Race
import com.dowstats.data.entities.addon.BuildingAddon
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.*

interface UnitRepository : CrudRepository<DowUnit, Long> {

    @Query("""
        select d, a1, a2
        from DowUnit d
            left join fetch ArmorType a1 on d.armorType.id = a1.id
            left join fetch ArmorType a2 on d.armorType2.id = a2.id
        where d.modId = :modId
        and (:race is null or d.race = :race)
        order by d.race.id desc
    """)
    fun findByModIdAndRace(modId: Long, race: Race?): List<DowUnit>

    fun findFirstByModIdAndFilenameSquad(modId: Long, fileName: String): DowUnit?

    fun deleteAllByModIdAndRaceId(modId: Long, raceId: String)
}