package com.gcsc.connection.monitoring.dto;

import com.gcsc.connection.monitoring.entity.SnpApiRequestLog;

import java.time.LocalDateTime;

public record RequestLogResponse(
    Long logId,
    String requestUrl,
    String requestMethod,
    String requestStatus,
    String requestIp,
    Integer responseStatus,
    Integer responseTime,
    Long responseSize,
    String errorMessage,
    LocalDateTime requestedAt,
    Long serviceId,
    String serviceName,
    String apiKeyPrefix,
    String userName,
    String requestHeaders,
    String requestParams
) {

    /**
     * 목록 조회용 변환 (헤더/파라미터 제외)
     */
    public static RequestLogResponse from(SnpApiRequestLog log) {
        return new RequestLogResponse(
            log.getLogId(),
            log.getRequestUrl(),
            log.getRequestMethod(),
            log.getRequestStatus(),
            log.getRequestIp(),
            log.getResponseStatus(),
            log.getResponseTime(),
            log.getResponseSize(),
            log.getErrorMessage(),
            log.getRequestedAt(),
            log.getService() != null ? log.getService().getServiceId() : null,
            log.getService() != null ? log.getService().getServiceName() : null,
            log.getApiKey() != null ? log.getApiKey().getApiKeyPrefix() : null,
            log.getUser() != null ? log.getUser().getUserName() : null,
            null,
            null
        );
    }

    /**
     * 상세 조회용 변환 (모든 필드 포함)
     */
    public static RequestLogResponse fromDetail(SnpApiRequestLog log) {
        return new RequestLogResponse(
            log.getLogId(),
            log.getRequestUrl(),
            log.getRequestMethod(),
            log.getRequestStatus(),
            log.getRequestIp(),
            log.getResponseStatus(),
            log.getResponseTime(),
            log.getResponseSize(),
            log.getErrorMessage(),
            log.getRequestedAt(),
            log.getService() != null ? log.getService().getServiceId() : null,
            log.getService() != null ? log.getService().getServiceName() : null,
            log.getApiKey() != null ? log.getApiKey().getApiKeyPrefix() : null,
            log.getUser() != null ? log.getUser().getUserName() : null,
            log.getRequestHeaders(),
            log.getRequestParams()
        );
    }
}
