package com.example.demo.directions.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.example.demo.directions.model.DirectionsEntity;
import com.example.demo.directions.model.DirectionsGrouped;

public interface DirectionsRepository extends CrudRepository<DirectionsEntity, Long>, PagingAndSortingRepository<DirectionsEntity, Long> {
    Optional<DirectionsEntity> findByNameIgnoreCase(String name);

    List<DirectionsEntity> findByDepartmentId(long departmentId);

    Page<DirectionsEntity> findByDepartmentId(long departmentId, Pageable pageable);

    Page<DirectionsEntity> findAll(Pageable pageable);
    @Query("select d.id as id, d.code as code, d.name as name, d.things as things, " +
            "d.department.name as departmentName " +
            "from DirectionsEntity d ")
    Page<DirectionsGrouped> findAllWithDepartment(Pageable pageable);
    @Query("select d.id as id, d.code as code, d.name as name, d.things as things, " +
            "d.department.name as departmentName " +
            "from DirectionsEntity d " +
            "where lower(name) like lower(concat('%', :nameS, '%'))")
    Page<DirectionsGrouped> findAllWithDepartmentSearch(Pageable pageable, String nameS);
}
