package com.zioinfo.mall.analytics;

import com.zioinfo.mall.analytics.mapper.AnalyticsMapper;
import com.zioinfo.mall.common.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 분석 API — /api/mall/analytics. MANAGER+.
 * Super Admin 대시보드(매장별 매출·순위·대량주문) + BI 매출 피드.
 */
@RestController
@RequestMapping("/api/mall/analytics")
@RequiredArgsConstructor
@PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
public class AnalyticsController {

    private final AnalyticsMapper mapper;

    /** Super Admin 실시간 현황판 — 오늘 총매출 + 매장별 순위 + 대량주문. */
    @GetMapping("/dashboard")
    public ApiResponse<Map<String, Object>> dashboard(
            @RequestParam(defaultValue = "1") int days,
            @RequestParam(defaultValue = "500") BigDecimal bigOrderThreshold) {
        Map<String, Object> out = new LinkedHashMap<>();
        out.put("today", mapper.todayTotals());
        out.put("storeRanking", mapper.salesByStore(days));
        out.put("topProducts", mapper.topProducts(10));
        out.put("bigOrders", mapper.bigOrders(bigOrderThreshold, 20));
        return ApiResponse.ok(out);
    }

    @GetMapping("/store-sales")
    public ApiResponse<List<Map<String, Object>>> storeSales(@RequestParam(defaultValue = "7") int days) {
        return ApiResponse.ok(mapper.salesByStore(days));
    }

    @GetMapping("/trend")
    public ApiResponse<List<Map<String, Object>>> trend(@RequestParam(defaultValue = "14") int days) {
        return ApiResponse.ok(mapper.dailyTrend(days));
    }

    @GetMapping("/top-products")
    public ApiResponse<List<Map<String, Object>>> topProducts(@RequestParam(defaultValue = "10") int limit) {
        return ApiResponse.ok(mapper.topProducts(limit));
    }
}
