package com.ga.core.mapper.commission;

import com.ga.core.vo.commission.AgentReserveFundResp;
import com.ga.core.vo.commission.AgentReserveFundSearchParam;
import com.ga.core.vo.commission.AgentReserveFundVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * agent_reserve_fund Mapper.
 * 적립금: 설계사별 월별 적립금 잔액 관리.
 * balance = 전월잔액 + reserve_in - reserve_out (Service 계산).
 */
@Mapper
public interface AgentReserveFundMapper {

    AgentReserveFundResp selectById(@Param("reserveId") Long reserveId);

    List<AgentReserveFundResp> selectList(AgentReserveFundSearchParam param);

    /**
     * 설계사 + 정산월 단건 조회 — 중복 방지 및 해당 월 잔액 확인용.
     */
    AgentReserveFundVO selectByAgentAndMonth(@Param("agentId") Long agentId,
                                             @Param("settleMonth") String settleMonth);

    /**
     * 설계사의 지정월 이전 최신(직전월) 레코드 1건 — 잔액 누적 계산용.
     * settleMonth 미만인 데이터 중 settle_month DESC LIMIT 1.
     */
    AgentReserveFundVO selectLatestBefore(@Param("agentId") Long agentId,
                                          @Param("settleMonth") String settleMonth);

    int insert(AgentReserveFundVO vo);

    int update(AgentReserveFundVO vo);

    int delete(@Param("reserveId") Long reserveId);
}
