package ru.soune.nocopy.repository;

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.file.FileUploadSession;
import ru.soune.nocopy.entity.file.UploadStatus;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@Repository
public interface FileUploadSessionRepository extends JpaRepository<FileUploadSession, String> {

    List<FileUploadSession> findByUserId(Long userId);

    List<FileUploadSession> findByStatus(UploadStatus status);

    @Query("SELECT s FROM FileUploadSession s WHERE s.status IN :statuses AND s.createdAt < :threshold")
    List<FileUploadSession> findByStatusInAndCreatedAtBefore(
            @Param("statuses") Set<UploadStatus> statuses,
            @Param("threshold") LocalDateTime threshold);

    @Query("SELECT s FROM FileUploadSession s WHERE s.expiresAt < :now AND s.status IN :statuses")
    List<FileUploadSession> findExpiredSessions(
            @Param("now") LocalDateTime now,
            @Param("statuses") Set<UploadStatus> statuses);


    @Query("SELECT s FROM FileUploadSession s WHERE s.filePath = :filePath AND s.status = :status AND s.userId = :userId")
    FileUploadSession findByFilePathAndStatus(
            @Param("filePath") String filePath,
            @Param("status") String status,
            @Param("userId") Long userId);
}
