package enea.lecservice.repository;

import enea.lecservice.entities.Page;
import enea.lecservice.entities.PermissionEnum;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.UUID;

public interface PageRepository extends JpaRepository<Page, UUID> {

    List<Page> findAllByDeletedFalseOrderByName();

    @Query(value = "select p from Page p where p.deleted = false and p.visible = true and lower(p.lang) = lower(:lang) and p.permissionId in(:permissions) order by p.showOrder asc ")
    List<Page> getPagesInPermissions(@Param("lang") String lang, @Param("permissions") List<String> permissions);

    @Query(value = "select p from Page p where p.deleted = false and p.visible = true and p.permissionId in (:permissions) and p.slug = :slug")
    List<Page> getPagesInPermissionsBySlugForAllLangs(String slug, @Param("permissions") List<String> permissions);

    @Query(value = "select case when count(p) > 0 then true else false end from Page p where lower(p.slug) = lower(:slug)")
    boolean slugExists(@Param("slug") String slug);

    @Query(value = "select p from Page p where p.deleted = false and p.permissionId = :permission " +
            "and (:query is null or lower(p.name) like lower(concat('%s', :query, '%'))) "
    )
    org.springframework.data.domain.Page<Page> getPaginatedData(@Param("query") String query, @Param("permission") PermissionEnum permission, Pageable pageRequest);
}

