package com.zioinfo.kintex.analytics;

import com.zioinfo.kintex.analytics.dto.AnalyticsDto;
import com.zioinfo.kintex.auth.EventAccessGuard;
import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 경영분석 BI API (SCR-13). 행사 멤버/홀매니저 열람.
 * perspective(operator|exhibitor)·period(event|d90|annual)로 집계 세트가 갈린다.
 */
@RestController
@RequestMapping("/api/events/{eventId}/analytics")
public class AnalyticsController {

    private final AnalyticsService service;
    private final EventAccessGuard guard;

    public AnalyticsController(AnalyticsService service, EventAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    /** GET — KPI·매출 추이·손익·(원천 있으면)업종·참가업체 성과. */
    @GetMapping
    public ApiResponse<AnalyticsDto> get(@AuthenticationPrincipal KintexPrincipal principal,
                                         @PathVariable String eventId,
                                         @RequestParam(required = false, defaultValue = "operator") String perspective,
                                         @RequestParam(required = false, defaultValue = "event") String period) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.getAnalytics(eventId, perspective, period));
    }
}
