package de.votian.services.repository;

import de.votian.services.entity.ServiceZipCodeAreaMapping;
import de.votian.services.entity.ServiceZipCodeAreaMappingId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

public interface ServiceZipCodeAreaMappingRepository extends JpaRepository<ServiceZipCodeAreaMapping, ServiceZipCodeAreaMappingId> {

    @Query(value = "SELECT srvpam.srvpa_id " +
                   "FROM serviceplzareamapping AS srvpam " +
                   "JOIN serviceplzarea AS srvpa ON srvpa.srvpa_id = srvpam.srvpa_id " +
                   "WHERE srvpam.srvp_id = :zipCodeId AND srvpa.hq_id = :hqId " +
                   "ORDER BY srvpam.srvpa_id " +
                   "LIMIT 1", nativeQuery = true)
    Optional<Long> findAreaIdByZipCodeIdAndHeadquartersId(@Param("zipCodeId") Long zipCodeId,
                                                          @Param("hqId") Long headquartersId);
}
