package com.ga.core.mapper.withdraw;

import com.ga.core.vo.withdraw.WithdrawRequestResp;
import com.ga.core.vo.withdraw.WithdrawRequestSearchParam;
import com.ga.core.vo.withdraw.WithdrawRequestVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface WithdrawRequestMapper {
    WithdrawRequestResp selectById(@Param("requestId") Long requestId);
    /** account_no 복호화 포함 — 펌뱅킹 송신 배치 전용 */
    WithdrawRequestVO selectVOById(@Param("requestId") Long requestId);
    List<WithdrawRequestResp> selectList(WithdrawRequestSearchParam param);
    int insert(WithdrawRequestVO vo);
    int updateStatus(@Param("requestId") Long requestId, @Param("status") String status,
                     @Param("failReason") String failReason);
    int markSent(@Param("requestId") Long requestId);
    int markCompleted(@Param("requestId") Long requestId);
    int cancel(@Param("requestId") Long requestId);

    /** 펌뱅킹 어댑터 응답을 저장 (transfer_tx_id/status/at + 거래상태에 따른 status 전이). */
    int updateTransferResult(@Param("requestId") Long requestId,
                             @Param("txId") String txId,
                             @Param("transferStatus") String transferStatus,
                             @Param("nextStatus") String nextStatus,
                             @Param("failReason") String failReason);

    /**
     * 펌뱅킹 재시도 대상 조회 — transfer_status='FAILED' 이고 status='APPROVED' 상태.
     * account_no 는 복호화되어 반환.
     */
    List<WithdrawRequestVO> selectFailedTransfers(@Param("limit") int limit);

    /**
     * SETTLED 동기화 대상 — transfer_status='ACCEPTED' 이고 status='SENT'.
     * queryStatus 폴링 결과로 입금 확정/실패를 판정한다.
     */
    List<WithdrawRequestVO> selectAcceptedTransfers(@Param("limit") int limit);

    /** queryStatus 가 SETTLED 응답: status='COMPLETED', transfer_status='SETTLED', completed_at=NOW(). */
    int markSettled(@Param("requestId") Long requestId);

    /** queryStatus 가 FAILED 응답: status='APPROVED'(재시도 풀로), transfer_status='FAILED', fail_reason. */
    int markSettleFailed(@Param("requestId") Long requestId,
                         @Param("failReason") String failReason);

    /**
     * 해당 settle_master 에서 CANCELLED/FAILED 외 활성 출금 신청의 amount 합계.
     * 출금 잔여 한도 검증 용도. null 가능(0건).
     */
    java.math.BigDecimal sumActiveAmountByMaster(@Param("settleMasterId") Long settleMasterId);
}
