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.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import ru.soune.nocopy.entity.file.FileAppeal;
import ru.soune.nocopy.entity.file.AppealStatus;
import java.util.List;
import java.util.Optional;

public interface FileAppealRepository extends JpaRepository<FileAppeal, String> {

    Optional<FileAppeal> findByFileIdAndStatus(String fileId, AppealStatus status);

    List<FileAppeal> findByFileId(String fileId);

    Page<FileAppeal> findByStatus(AppealStatus status, Pageable pageable);

    Page<FileAppeal> findByUserId(Long userId, Pageable pageable);

    @Query("SELECT fa FROM FileAppeal fa WHERE fa.status = :status ORDER BY fa.createdAt ASC")
    Page<FileAppeal> findPendingAppeals(@Param("status") AppealStatus status, Pageable pageable);

    boolean existsByFileIdAndStatusIn(String fileId, List<AppealStatus> statuses);

    @Modifying
    @Transactional
    @Query("DELETE FROM FileAppeal a WHERE a.fileId IN :fileIds")
    void deleteAllByFileIdIn(@Param("fileIds") List<String> fileIds);
}

