package com.example.demo.entrysData.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.stereotype.Repository;

import com.example.demo.entrysData.model.EntrysDataEntity;
import com.example.demo.entrysData.model.EntrysDataGrouped;
import com.example.demo.entrysData.model.EntrysDataGroupedDepartment;

@Repository
public interface EntrysDataRepository extends CrudRepository<EntrysDataEntity, Long> {
    Optional<EntrysDataEntity> findByloginIgnoreCase(String name);

    List<EntrysDataEntity> findByDepartmentId(long departmentId);

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

    Page<EntrysDataEntity> findAll(Pageable pageable);

    @Query("SELECT e.department.name AS departmentName, COUNT(e.id) AS count " +
            "FROM EntrysDataEntity e " +
            "GROUP BY departmentName ORDER BY count DESC")
    List<EntrysDataGrouped> getCountEntrysInDepartment();
    
    @Query("SELECT e.department.name AS departmentName, e.login AS login, e.role AS role, e.id AS id " +
            "FROM EntrysDataEntity e " +
            "ORDER BY id DESC")
    Page<EntrysDataGroupedDepartment> findAllWithDepartment(Pageable pageable);
}
