package de.votian.services.repository;

import de.votian.services.entity.CourierVehicle;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
import java.util.Optional;

public interface CourierVehicleRepository extends JpaRepository<CourierVehicle, Long> {

    Optional<CourierVehicle> findBySid(String sid);

    List<CourierVehicle> findByCourierId(Long courierId);

    List<CourierVehicle> findByCourierIdIn(List<Long> courierIds);

    @Query("SELECT cv FROM CourierVehicle cv WHERE cv.sid = :sid AND cv.id <> :excludeId")
    Optional<CourierVehicle> findBySidExcludingId(@Param("sid") String sid, @Param("excludeId") Long excludeId);

    @Query("SELECT cv FROM CourierVehicle cv JOIN Courier cr ON cv.courierId = cr.id " +
           "WHERE cr.headquartersId = :hqId AND cv.sid LIKE %:search% ORDER BY cv.sid")
    List<CourierVehicle> searchBySidAndHq(@Param("hqId") Long hqId, @Param("search") String search);

    @Query("SELECT cv FROM CourierVehicle cv JOIN Courier cr ON cv.courierId = cr.id " +
           "WHERE cr.headquartersId = :hqId ORDER BY cr.sid, cv.sid")
    List<CourierVehicle> findByHeadquartersId(@Param("hqId") Long hqId);
}
