package com.zioinfo.kintex.analytics;

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

/**
 * 테넌트 전역(행사 횡단) 경영분석 BI (M16 잔여 갭 — SCR-13).
 * 행사 간 매출 비교·홀 가동률 추이·참가사 리텐션·수요예측(추세). 홀매니저(킨텍스 내부) 전용 열람.
 * 행사 스코프 집계는 {@link AnalyticsController}(/api/events/{id}/analytics)가 담당한다.
 */
@RestController
@RequestMapping("/api/analytics")
public class AnalyticsOverviewController {

    private final AnalyticsService service;
    private final EventAccessGuard guard;

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

    /** GET — 전역 KPI·행사 간 매출 비교·홀 가동률 추이·리텐션·수요예측. 홀매니저만 허용. */
    @GetMapping("/overview")
    public ApiResponse<AnalyticsOverviewDto> overview(
            @AuthenticationPrincipal KintexPrincipal principal,
            @RequestParam(required = false, defaultValue = "5") int years) {
        guard.require(principal);
        if (!principal.hallManager()) {
            throw new ApiException(ErrorCode.FORBIDDEN);
        }
        return ApiResponse.ok(service.getOverview(years));
    }
}
