package cz.muni.fi.pa165.soccermanager.repository;

import cz.muni.fi.pa165.soccermanager.entity.Team;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface TeamRepository extends JpaRepository<Team, Long> {

  @Query("SELECT t FROM Team t WHERE t.league.id = :id")
  List<Team> findByLeague(Long id);

  @Query("SELECT t FROM Team t WHERE t.championship.id = :id")
  List<Team> findByChampionship(Long id);

  @Query("SELECT " +
          "CASE " +
          "WHEN COUNT( t ) > 0  THEN true " +
          "ELSE " +
          "false " +
          "END " +
          "FROM Team t WHERE t.name = :name"
  )
  Boolean teamNameExists(String name);

  @Query("SELECT t FROM Team t WHERE t.user.id = :userId")
  Optional<Team> findByUserId(Long userId);
}
