package org.jpos.tcpay.db.repository;

import org.jpos.tcpay.db.entity.PosTransactionReversal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * Repository interface for PosTransactionReversal entities
 * Defines contract for reversal transaction database operations
 */
public interface PosTransactionReversalRepository {
    
    /**
     * Save a reversal transaction to the database
     * @param reversal the entity to save
     * @return the saved entity with generated ID
     */
    PosTransactionReversal save(PosTransactionReversal reversal);
    
    /**
     * Find reversal by ID
     * @param id the reversal ID
     * @return Optional containing the entity if found
     */
    Optional<PosTransactionReversal> findById(Long id);
    
    /**
     * Find reversals by original temp transaction ID
     * @param originalTempTxnId the original temp transaction ID
     * @return list of reversals for the original transaction
     */
    List<PosTransactionReversal> findByOriginalTempTxnId(Long originalTempTxnId);
    
    /**
     * Check if reversal exists for given temp transaction
     * @param originalTempTxnId the original temp transaction ID
     * @return true if reversal exists
     */
    boolean existsByOriginalTempTxnId(Long originalTempTxnId);
    
    /**
     * Find reversals by status
     * @param statuses list of statuses to filter by
     * @return list of matching reversals
     */
    List<PosTransactionReversal> findByStatusIn(List<PosTransactionReversal.ReversalStatus> statuses);
    
    /**
     * Count pending reversals by status
     * @param statuses list of statuses to count
     * @return count of reversals with matching statuses
     */
    long countByStatusIn(List<PosTransactionReversal.ReversalStatus> statuses);
    
    /**
     * Find reversals ready for retry
     * @param currentTime current timestamp
     * @return list of reversals ready for retry
     */
    List<PosTransactionReversal> findReversalsForRetry(LocalDateTime currentTime);
    
    /**
     * Find reversals by bank terminal and merchant IDs
     * @param bTid bank terminal ID
     * @param bMid bank merchant ID
     * @return list of matching reversals
     */
    List<PosTransactionReversal> findByBankTidAndBankMid(String bTid, String bMid);
    
    /**
     * Count all reversals
     * @return total count
     */
    long count();
}