package com.eactive.eai.rms.onl.apim.obp;

import com.eactive.apim.portal.obp.entity.ObpGwMetric;
import com.google.gson.annotations.SerializedName;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * ObpGwMetric JSON 응답 DTO (after-timeslice 엔드포인트용)
 *
 * <p>예시 출력 형식에 맞춤:</p>
 * <pre>
 * {
 *   "apiId": "1ba56b82-a9c9-41e2-b3ab-ff9377667d56",
 *   "apiName": "[해외송금] 가상계좌 입금실패 정보조회",
 *   "apiRoutingUri": "/api/obs/remittance/remittancefail*",
 *   "httpMethod": "POST",
 *   "orgId": "ORG001",
 *   "partnerCode": "000008-01",
 *   "appId": "APP001",
 *   "appKey": "l7xx2fd7efc523794be5a1b126873a5de613",
 *   "requestUri": "-1",
 *   "timeslice": "2025-12-02T03:00:00.000+0000",
 *   "attemptedCount": 3,
 *   "completedCount": 3
 * }
 * </pre>
 */
@Data
public class ObpGwMetricDTO {

    private static final DateTimeFormatter ISO8601_MILLIS_FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'+0900'");

    @SerializedName("apiId")
    private String apiId;

    @SerializedName("apiName")
    private String apiName;

    @SerializedName("apiRoutingUri")
    private String apiRoutingUri;

    @SerializedName("httpMethod")
    private String httpMethod;

    @SerializedName("orgId")
    private String orgId;

    @SerializedName("partnerCode")
    private String partnerCode;

    @SerializedName("appId")
    private String appId;

    @SerializedName("appKey")
    private String appKey;

    @SerializedName("requestUri")
    private String requestUri;

    @SerializedName("timeslice")
    private String timeslice;

    @SerializedName("attemptedCount")
    private Long attemptedCount;

    @SerializedName("completedCount")
    private Long completedCount;


    // AS-IS 호환을 위한 필드 (null 값으로 리턴)
    @SerializedName("clusterHostname")
    private String clusterHostname = null;

    @SerializedName("apiDatabaseId")
    private Integer apiDatabaseId = null;

    @SerializedName("unauthorizedCount")
    private Integer authorizedCount = null;

    @SerializedName("backMinTime")
    private Integer backMinTime = null;

    @SerializedName("backMaxTime")
    private Integer backMaxTime = null;

    @SerializedName("backSumTime")
    private Integer backSumTime = null;

    @SerializedName("frontMinTime")
    private Integer frontMinTime = null;

    @SerializedName("frontMaxTime")
    private Integer frontMaxTime = null;

    @SerializedName("frontSumTime")
    private Integer frontSumTime = null;


    /**
     * Entity → DTO 변환
     */
    public static ObpGwMetricDTO from(ObpGwMetric entity) {
        ObpGwMetricDTO dto = new ObpGwMetricDTO();
        dto.setApiId(entity.getApiId());
        dto.setApiName(entity.getApiName());
        dto.setApiRoutingUri(entity.getUri());
        dto.setHttpMethod(entity.getMethod());
        dto.setOrgId(entity.getOrgId());
        dto.setPartnerCode(entity.getPartnerCode());
        dto.setAppId(entity.getAppId());
        dto.setAppKey(entity.getClientId());
        dto.setRequestUri(entity.getRequestUri());
        dto.setTimeslice(formatTimeslice(entity.getTimeslice()));
        dto.setAttemptedCount(entity.getAttemptedCount());
        dto.setCompletedCount(entity.getCompletedCount());


        // 260224 - AS-IS 개발자포탈에서 쓰이던 ID 필드 값 삭제(OBP 디비 필드 형식과 맞지 않음)
        dto.setOrgId(null);
        dto.setAppId(null);

        return dto;
    }

    /**
     * timeslice를 ISO8601 형식 (밀리초 포함) + UTC 시간대로 변환
     * 예: 2025-12-02T03:00:00.000+0000
     */
    private static String formatTimeslice(LocalDateTime timeslice) {
        if (timeslice == null) return null;
        return timeslice.format(ISO8601_MILLIS_FORMATTER);
    }
}
