package com.kt.event.analytics.controller;

import com.kt.event.analytics.dto.response.UserRoiAnalyticsResponse;
import com.kt.event.analytics.service.UserRoiAnalyticsService;
import com.kt.event.common.dto.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * User ROI Analytics Controller
 */
@Tag(name = "User ROI", description = "사용자 전체 이벤트 ROI 분석 API")
@Slf4j
@RestController
@RequestMapping("/api/v1/users")
@RequiredArgsConstructor
public class UserRoiAnalyticsController {

    private final UserRoiAnalyticsService userRoiAnalyticsService;

    @Operation(
        summary = "사용자 전체 ROI 상세 분석",
        description = "사용자의 모든 이벤트 ROI를 통합하여 분석합니다."
    )
    @GetMapping("/{userId}/analytics/roi")
    public ResponseEntity<ApiResponse<UserRoiAnalyticsResponse>> getUserRoiAnalytics(
        @Parameter(description = "사용자 ID", required = true)
        @PathVariable String userId,

        @Parameter(description = "예상 수익 포함 여부")
        @RequestParam(required = false, defaultValue = "true")
        Boolean includeProjection,

        @Parameter(description = "조회 시작 날짜")
        @RequestParam(required = false)
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
        LocalDateTime startDate,

        @Parameter(description = "조회 종료 날짜")
        @RequestParam(required = false)
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
        LocalDateTime endDate,

        @Parameter(description = "캐시 갱신 여부")
        @RequestParam(required = false, defaultValue = "false")
        Boolean refresh
    ) {
        log.info("사용자 ROI 분석 API 호출: userId={}, includeProjection={}", userId, includeProjection);

        UserRoiAnalyticsResponse response = userRoiAnalyticsService.getUserRoiAnalytics(
            userId, includeProjection, startDate, endDate, refresh
        );

        return ResponseEntity.ok(ApiResponse.success(response));
    }
}
