package com.xy.xyaicpzs.controller;

import com.xy.xyaicpzs.common.response.ApiResponse;
import com.xy.xyaicpzs.service.PageViewService;
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 org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 页面访问量统计控制器
 */
@RestController
@RequestMapping("/pv")
@RequiredArgsConstructor
@Tag(name = "页面访问量统计", description = "PV统计相关接口")
public class PageViewController {

    private final PageViewService pageViewService;

    @PostMapping("/record")
    @Operation(summary = "记录页面访问", description = "记录一次页面访问")
    public ApiResponse<String> recordPageView() {
        pageViewService.incrementPageView();
        return ApiResponse.success("记录成功");
    }

    @GetMapping("/total")
    @Operation(summary = "获取总PV", description = "获取网站累计总访问量")
    public ApiResponse<Long> getTotalPageViews() {
        return ApiResponse.success(pageViewService.getTotalPageViews());
    }

    @GetMapping("/today")
    @Operation(summary = "获取今日PV", description = "获取今日访问量")
    public ApiResponse<Long> getTodayPageViews() {
        return ApiResponse.success(pageViewService.getTodayPageViews());
    }

    @GetMapping("/stats")
    @Operation(summary = "根据天数获取PV统计", description = "获取近N天的PV统计（最大90天）")
    public ApiResponse<Map<String, Long>> getPageViewsByDays(
            @Parameter(description = "天数：7/30/90，默认7天，最大90天") @RequestParam(defaultValue = "7") int days) {
        return ApiResponse.success(pageViewService.getPageViewsByDays(days));
    }
}
