package kr.co.accura.wtm.domain.approval.repository;

import kr.co.accura.wtm.domain.approval.entity.TtApprovalLine;
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 java.util.List;
import java.util.Optional;

public interface TtApprovalLineRepository extends JpaRepository<TtApprovalLine, Long> {

    List<TtApprovalLine> findByApproval_IdOrderByApprovalOrder(Long approvalId);

    @Query("SELECT l FROM TtApprovalLine l WHERE l.approval.id = :approvalId " +
           "AND l.approvalOrder > :currentOrder AND l.status = 'PENDING' " +
           "ORDER BY l.approvalOrder ASC")
    Optional<TtApprovalLine> findNextPending(
            @Param("approvalId") Long approvalId,
            @Param("currentOrder") int currentOrder);

    @Query("SELECT l FROM TtApprovalLine l WHERE l.approverId = :approverId AND l.status = 'PENDING'")
    Page<TtApprovalLine> findPendingByApproverId(@Param("approverId") Long approverId, Pageable pageable);

    @Query("SELECT l FROM TtApprovalLine l WHERE l.approverId = :approverId AND l.status <> 'PENDING'")
    Page<TtApprovalLine> findHistoryByApproverId(@Param("approverId") Long approverId, Pageable pageable);
}
