package kr.xit.biz.ens.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import kr.xit.biz.ens.cmm.CmmEnsBizUtils;
import kr.xit.biz.ens.model.kt.KtCommonDTO;
import kr.xit.core.exception.BizRuntimeException;
import kr.xit.core.model.ApiResponseDTO;
import kr.xit.core.service.AbstractService;
import kr.xit.core.spring.util.ApiWebClientUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <pre>
 * description : 모바일 전자고지 배치 서비스
 *               - 배치에서 호출되는 클래스로 배치 로그등 Transaction 분리 필요
 * packageName : kr.xit.biz.ens.service
 * fileName    : EnsBatchKtGbsAccessTokenUpdateService
 * author      : limju
 * date        : 2024-08-29
 * ======================================================================
 * 변경일         변경자        변경 내용
 * ----------------------------------------------------------------------
 * 2024-08-29    limju       최초 생성
 *
 * </pre>
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EnsBatchKtGbsAccessTokenUpdateService extends AbstractService implements IEnsBatchKtGbsAccessTokenUpdateService {
    @Value("${app.contract.host}")
    private String apiHost;

    @Value("${app.contract.kt.gibis.api.requestToken}")
    private String requestToken;

    private final ApiWebClientUtil apiWebClient;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void modifyAccessToken(final KtCommonDTO.KtMnsRequest dto) {
        final String url = apiHost + requestToken;

        var result = apiWebClient.exchange(
                url,
                HttpMethod.POST,
                dto,
                ApiResponseDTO.class,
                CmmEnsBizUtils.getHeadeMap());

        if(!result.isSuccess()){
            throw BizRuntimeException.create(result.getCode(), result.getMessage());
        }
    }
}
