"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const dashboardService_1 = __importDefault(require("../services/dashboardService"));
class DashboardController {
    /**
     * 获取 KPI 数据：日活用户数、日活曲线、广告收益、收益曲线
     * GET /api/dashboard/kpi?dateRange=7
     * @param req Express 请求对象
     * @param res Express 响应对象
     */
    async getKpi(req, res) {
        try {
            const dateRange = Math.min(Math.max(parseInt(req.query.dateRange) || 7, 1), 90); // 1-90 天
            console.log(`[DashboardController] getKpi start, dateRange=${dateRange}`);
            const startedAt = Date.now();
            const kpiData = await dashboardService_1.default.getKpi(dateRange);
            console.log(`[DashboardController] getKpi end, durationMs=${Date.now() - startedAt}`);
            res.status(200).json({
                success: true,
                data: kpiData,
            });
        }
        catch (error) {
            console.error("[DashboardController] Error in getKpi:", error);
            res.status(500).json({
                success: false,
                message: "Failed to fetch KPI data",
                error: error.message,
            });
        }
    }
    /**
     * 获取作品统计数据：点击率、完成率排行
     * GET /api/dashboard/artwork-stats?top=10&sortBy=clickRate
     * @param req Express 请求对象
     * @param res Express 响应对象
     */
    async getArtworkStats(req, res) {
        try {
            const top = Math.min(Math.max(parseInt(req.query.top) || 10, 1), 100); // 1-100 件
            const sortBy = req.query.sortBy || "clickCount"; // clickRate | completionRate | clickCount | completionCount
            // 验证 sortBy 值
            const validSortByValues = ["clickRate", "completionRate", "clickCount", "completionCount"];
            if (!validSortByValues.includes(sortBy)) {
                res.status(400).json({
                    success: false,
                    message: `Invalid sortBy value. Expected one of: ${validSortByValues.join(", ")}`,
                });
                return;
            }
            console.log(`[DashboardController] getArtworkStats start, top=${top}, sortBy=${sortBy}`);
            const startedAt = Date.now();
            const artworkStats = await dashboardService_1.default.getArtworkStats(top, sortBy);
            console.log(`[DashboardController] getArtworkStats end, durationMs=${Date.now() - startedAt}`);
            res.status(200).json({
                success: true,
                data: artworkStats,
            });
        }
        catch (error) {
            console.error("[DashboardController] Error in getArtworkStats:", error);
            res.status(500).json({
                success: false,
                message: "Failed to fetch artwork stats",
                error: error.message,
            });
        }
    }
    /**
     * 获取最近 N 天上新作品分日数据（用于 tab 展示）
     * GET /api/dashboard/new-artwork-tabs?days=7&topPerDay=20
     */
    async getNewArtworkTabs(req, res) {
        try {
            const days = Math.min(Math.max(parseInt(req.query.days) || 7, 1), 30);
            const topPerDay = Math.min(Math.max(parseInt(req.query.topPerDay) || 20, 1), 100);
            console.log(`[DashboardController] getNewArtworkTabs start, days=${days}, topPerDay=${topPerDay}`);
            const startedAt = Date.now();
            const data = await dashboardService_1.default.getNewArtworkTabs(days, topPerDay);
            console.log(`[DashboardController] getNewArtworkTabs end, durationMs=${Date.now() - startedAt}`);
            res.status(200).json({
                success: true,
                data,
            });
        }
        catch (error) {
            console.error("[DashboardController] Error in getNewArtworkTabs:", error);
            res.status(500).json({
                success: false,
                message: "Failed to fetch new artwork tabs",
                error: error.message,
            });
        }
    }
}
const dashboardController = new DashboardController();
exports.default = dashboardController;
