package com.eactive.ext.kjb.statistics.ui;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * API 통계 차트 데이터 DTO (시간대별 집계)
 */
@Data
@NoArgsConstructor
public class ApiStatsChartUI {
	private String statTime;

	// 건수 메트릭 (SUM)
	private Long totalCnt;
	private Long successCnt;
	private Long timeoutCnt;
	private Long systemErrCnt;
	private Long bizErrCnt;
	private Long seq900TimeoutCnt;
	private Long seq900SystemErrCnt;
	private Long seq900BizErrCnt;

	// 응답시간 메트릭 (AVG)
	private BigDecimal avgRespTime;
	private BigDecimal p50RespTime;
	private BigDecimal p95RespTime;

	// QueryDSL Projections용 생성자 - Hour/Minute (LocalDateTime)
	public ApiStatsChartUI(LocalDateTime statTime, Long totalCnt, Long successCnt,
			Long timeoutCnt, Long systemErrCnt, Long bizErrCnt,
			Long seq900TimeoutCnt, Long seq900SystemErrCnt, Long seq900BizErrCnt,
			Double avgRespTime, Double p50RespTime, Double p95RespTime) {
		this.statTime = statTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
		setMetrics(totalCnt, successCnt, timeoutCnt, systemErrCnt, bizErrCnt,
				seq900TimeoutCnt, seq900SystemErrCnt, seq900BizErrCnt,
				avgRespTime, p50RespTime, p95RespTime);
	}

	// QueryDSL Projections용 생성자 - Day (LocalDate)
	public ApiStatsChartUI(LocalDate statTime, Long totalCnt, Long successCnt,
			Long timeoutCnt, Long systemErrCnt, Long bizErrCnt,
			Long seq900TimeoutCnt, Long seq900SystemErrCnt, Long seq900BizErrCnt,
			Double avgRespTime, Double p50RespTime, Double p95RespTime) {
		this.statTime = statTime.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
		setMetrics(totalCnt, successCnt, timeoutCnt, systemErrCnt, bizErrCnt,
				seq900TimeoutCnt, seq900SystemErrCnt, seq900BizErrCnt,
				avgRespTime, p50RespTime, p95RespTime);
	}

	// QueryDSL Projections용 생성자 - Month/Year (String)
	public ApiStatsChartUI(String statTime, Long totalCnt, Long successCnt,
			Long timeoutCnt, Long systemErrCnt, Long bizErrCnt,
			Long seq900TimeoutCnt, Long seq900SystemErrCnt, Long seq900BizErrCnt,
			Double avgRespTime, Double p50RespTime, Double p95RespTime) {
		this.statTime = statTime;
		setMetrics(totalCnt, successCnt, timeoutCnt, systemErrCnt, bizErrCnt,
				seq900TimeoutCnt, seq900SystemErrCnt, seq900BizErrCnt,
				avgRespTime, p50RespTime, p95RespTime);
	}

	private void setMetrics(Long totalCnt, Long successCnt, Long timeoutCnt,
			Long systemErrCnt, Long bizErrCnt, Long seq900TimeoutCnt,
			Long seq900SystemErrCnt, Long seq900BizErrCnt,
			Double avgRespTime, Double p50RespTime, Double p95RespTime) {
		this.totalCnt = totalCnt != null ? totalCnt : 0L;
		this.successCnt = successCnt != null ? successCnt : 0L;
		this.timeoutCnt = timeoutCnt != null ? timeoutCnt : 0L;
		this.systemErrCnt = systemErrCnt != null ? systemErrCnt : 0L;
		this.bizErrCnt = bizErrCnt != null ? bizErrCnt : 0L;
		this.seq900TimeoutCnt = seq900TimeoutCnt != null ? seq900TimeoutCnt : 0L;
		this.seq900SystemErrCnt = seq900SystemErrCnt != null ? seq900SystemErrCnt : 0L;
		this.seq900BizErrCnt = seq900BizErrCnt != null ? seq900BizErrCnt : 0L;
		this.avgRespTime = avgRespTime != null ? BigDecimal.valueOf(avgRespTime) : BigDecimal.ZERO;
		this.p50RespTime = p50RespTime != null ? BigDecimal.valueOf(p50RespTime) : BigDecimal.ZERO;
		this.p95RespTime = p95RespTime != null ? BigDecimal.valueOf(p95RespTime) : BigDecimal.ZERO;
	}
}
