package org.jpos.tcpay.db.repository;

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

/**
 * Repository interface for PosTempTransaction entities
 * Defines contract for temp transaction database operations
 */
public interface PosTempTransactionRepository {
    
    /**
     * Save a temp transaction to the database
     * @param tempTransaction the entity to save
     * @return the saved entity with generated ID
     */
    PosTempTransaction save(PosTempTransaction tempTransaction);
    
    /**
     * Find temp transaction by ID
     * @param id the temp transaction ID
     * @return Optional containing the entity if found
     */
    Optional<PosTempTransaction> findById(Long id);
    
    /**
     * Find temp transaction by bank terminal and merchant IDs
     * @param bTid bank terminal ID
     * @param bMid bank merchant ID
     * @return Optional containing the entity if found
     */
    Optional<PosTempTransaction> findByBankTidAndBankMid(String bTid, String bMid);
    
    /**
     * Check if terminal is busy (has existing temp transaction)
     * @param bTid bank terminal ID
     * @param bMid bank merchant ID
     * @param acquirerId bank/Acquirer ID
     * @return true if terminal has existing temp transaction
     */
    boolean existsByBankTidAndBankMidAndAcquirerId(String bTid, String bMid, Long acquirerId);
    
    /**
     * Find stale temp transactions older than cutoff time
     * @param cutoffTime the cutoff timestamp
     * @return list of stale transactions
     */
    List<PosTempTransaction> findStaleTransactions(LocalDateTime cutoffTime);
    
    /**
     * Count all temp transactions
     * @return total count
     */
    long count();
    
    /**
     * Count temp transactions older than cutoff time
     * @param cutoffTime the cutoff timestamp
     * @return count of stale transactions
     */
    long countStaleTransactions(LocalDateTime cutoffTime);
    
    /**
     * Delete temp transaction by ID
     * @param id the temp transaction ID
     */
    void deleteById(Long id);

    void delete(PosTempTransaction tempTransaction);

    /**
     * Update temp transaction status
     * @param id the temp transaction ID
     * @param status the new status
     */
    void updateStatus(Long id, PosTempTransaction.TempTransactionStatus status);
}