package com.example.demo.controller;

import com.example.demo.model.ApiResult;
import com.example.demo.service.StatService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 统计分析控制器。
 * <p>提供数据汇总、图表数据、自定义查询、报表导出及台账生成接口。</p>
 */
@RestController
@RequestMapping("/api/stat")
@Tag(name = "统计分析", description = "统计分析模块接口（需求标识：SRS-F-03-001~SRS-F-03-005）")
public class StatController {

    private final StatService statService;

    public StatController(StatService statService) {
        this.statService = statService;
    }

    /**
     * 数据汇总。
     *
     * @requirement(name="数据汇总", id="SRS-F-03-001")
     */
    @PostMapping("/summary")
    @Operation(summary = "数据汇总", description = "按维度和时间范围对业务数据进行聚合计算")
    public ApiResult<Map<String, Object>> summary(@RequestParam(required = false) String dimension,
                                                   @RequestParam(required = false) String dateFrom,
                                                   @RequestParam(required = false) String dateTo) {
        return ApiResult.success(statService.summary(dimension, dateFrom, dateTo));
    }

    /**
     * 图表数据。
     *
     * @requirement(name="图表展示", id="SRS-F-03-002")
     */
    @PostMapping("/chart")
    @Operation(summary = "图表数据", description = "以柱状图、折线图、饼图等形式展示统计结果")
    public ApiResult<Map<String, Object>> chart(@RequestParam(required = false) String chartType,
                                                 @RequestParam(required = false) String dimension,
                                                 @RequestParam(required = false) String dateFrom,
                                                 @RequestParam(required = false) String dateTo) {
        return ApiResult.success(statService.chartData(chartType, dimension, dateFrom, dateTo));
    }

    /**
     * 自定义查询。
     *
     * @requirement(name="自定义查询", id="SRS-F-03-003")
     */
    @PostMapping("/custom")
    @Operation(summary = "自定义查询", description = "组合查询条件进行灵活统计分析")
    public ApiResult<Map<String, Object>> custom(@RequestBody Map<String, String> conditions) {
        return ApiResult.success(statService.customQuery(conditions));
    }

    /**
     * 报表导出。
     *
     * @requirement(name="报表导出", id="SRS-F-03-004")
     */
    @PostMapping("/export")
    @Operation(summary = "报表导出", description = "将统计结果以Excel/PDF格式导出")
    public ApiResult<Map<String, Object>> export(@RequestParam(required = false) String format,
                                                  @RequestParam(required = false) String dateFrom,
                                                  @RequestParam(required = false) String dateTo) {
        return ApiResult.success(statService.exportReport(format, dateFrom, dateTo));
    }

    /**
     * 台账生成。
     *
     * @requirement(name="数据台账生成", id="SRS-F-03-005")
     */
    @PostMapping("/ledger")
    @Operation(summary = "台账生成", description = "按业务类型或时间段生成结构化数据清单")
    public ApiResult<Map<String, Object>> ledger(@RequestParam(required = false) String businessType,
                                                  @RequestParam(required = false) String dateFrom,
                                                  @RequestParam(required = false) String dateTo) {
        return ApiResult.success(statService.generateLedger(businessType, dateFrom, dateTo));
    }
}
