package de.votian.services.repository;

import de.votian.services.entity.Company;
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 CompanyRepository extends JpaRepository<Company, Long> {

    Optional<Company> findByComp(String comp);

    List<Company> findByArchivedNot(String archived);

    @Query("SELECT c FROM Company c WHERE c.comp LIKE %:search% OR c.match LIKE %:search%")
    List<Company> searchByCompOrMatch(@Param("search") String search);

    @Query("SELECT c FROM Company c JOIN Courier cr ON c.id = cr.companyId WHERE c.comp = :comp AND cr.headquartersId = :hqId")
    Optional<Company> findByCompAndCourierHq(@Param("comp") String comp, @Param("hqId") Long hqId);
}
