package com.ga.api.service.external;

import com.ga.common.model.PageResponse;
import com.ga.core.mapper.external.ExternalCallLogMapper;
import com.ga.core.vo.external.ExternalCallLogResp;
import com.ga.core.vo.external.ExternalCallLogSearchParam;
import com.ga.core.vo.external.ExternalCallLogVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 외부 연동 호출 감사 이력.
 *
 * <p>{@link #recordCall} 은 AOP({@link com.ga.api.aop.ExternalCallLoggingAspect})에서 호출된다.
 * 호출자 트랜잭션 롤백과 무관하게 영속화돼야 하므로 {@link Propagation#REQUIRES_NEW}.
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ExternalCallLogService {

    private final ExternalCallLogMapper mapper;

    /** AOP에서 호출. 호출자 트랜잭션과 분리(REQUIRES_NEW). */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void recordCall(ExternalCallLogVO vo) {
        mapper.insertOne(vo);
    }

    @Transactional(readOnly = true)
    public PageResponse<ExternalCallLogResp> list(ExternalCallLogSearchParam param) {
        param.startPage();
        return PageResponse.of(mapper.selectList(param));
    }

    @Transactional(readOnly = true)
    public ExternalCallLogResp detail(Long logId) {
        return mapper.selectById(logId);
    }
}
