package ru.soune.nocopy.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import ru.soune.nocopy.entity.complaint.LawCase;
import ru.soune.nocopy.entity.complaint.LawCasePriority;
import ru.soune.nocopy.entity.complaint.LawCaseType;

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

@Repository
public interface LawCaseRepository extends JpaRepository<LawCase, Long> {

    @Query("SELECT l FROM LawCase l WHERE l.user.Id = :userId")
    List<LawCase> getUserLawCases(@Param("userId") Long userId, Pageable pageable);

//    @Query("SELECT l FROM LawCase l WHERE l.user.Id = :userId " +
//            "AND (:type IS NULL OR l.type = :type) " +
//            "AND (:lawyer IS NULL OR l.lawyer = :lawyer) " +
//            "AND (:priority IS NULL OR l.priority = :priority)")
//    List<LawCase> getUserLawCases(@Param("userId") Long userId,
//                                  @Param("type") LawCaseType type,
//                                  @Param("lawyer") String lawyer,
//                                  @Param("priority") LawCasePriority priority,
//                                  Pageable pageable);

    @Query("SELECT l FROM LawCase l WHERE l.user.Id = :userId " +
            "AND (:type IS NULL OR l.type = :type) " +
            "AND (:lawyer IS NULL OR l.lawyer = :lawyer) " +
            "AND (:violationId IS NULL OR l.violationId = :violationId) " +
            "AND (:priority IS NULL OR l.priority = :priority)")
    Page<LawCase> getUserLawCases(@Param("userId") Long userId,
                                  @Param("type") LawCaseType type,
                                  @Param("lawyer") String lawyer,
                                  @Param("priority") LawCasePriority priority,
                                  @Param("violationId") Long violationId,
                                  Pageable pageable);

    @Query("SELECT COUNT(lc.id) FROM LawCase lc")
    Long getTotalLawCasesCount();

    @Query("SELECT COUNT(l) FROM LawCase l WHERE l.violationId IN :violationIds")
    Long countLawCaseByViolationIds(@Param("violationIds") List<Long> violationIds);

    Optional<LawCase> getLawCaseByViolationIdAndType(Long violationId, LawCaseType type);
}
