package de.avatic.taric.repository;

import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;

import de.avatic.taric.model.GeoGroup;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface GeoGroupRepository extends CrudRepository<GeoGroup, Integer> {

    @Query("SELECT * FROM geo_group WHERE geo_group_code = :code")
    Optional<GeoGroup> findByGeoGroupCode(@Param("code") String code);

    @Query("""
        SELECT * FROM geo_group AS gg
            LEFT JOIN geo_group_membership AS ggm ON gg.id = ggm.geo_group_id
            LEFT JOIN geo AS g ON ggm.geo_id = g.id
            WHERE iso_3166_code = :countryCode""")
    List<GeoGroup> findByCountryCode(@Param("countryCode") String countryCode);

    List<GeoGroup> findByAbbr(String abbr);
}
