package com.example.demo.controller;

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

import java.util.List;
import java.util.Map;

/**
 * 统计分析控制器。
 * <p>提供数据汇总、图表展示、自定义查询、报表导出及数据台账生成 RESTful API。</p>
 */
@Tag(name = "统计分析", description = "数据汇总、图表展示、自定义查询、报表导出、台账生成")
@RestController
@RequestMapping("/api/stat")
public class StatisticsController {

    private final StatisticsService statisticsService;

    public StatisticsController(StatisticsService statisticsService) {
        this.statisticsService = statisticsService;
    }

    /**
     * 数据汇总。
     *
     * @requirement(name="数据汇总", id="SRS-F-03-001")
     */
    @Operation(summary = "数据汇总")
    @PostMapping("/summary")
    public ApiResult<Map<String, Object>> summary() {
        return ApiResult.success(statisticsService.summary());
    }

    /**
     * 图表数据。
     *
     * @requirement(name="图表展示", id="SRS-F-03-002")
     */
    @Operation(summary = "图表数据")
    @PostMapping("/chart")
    public ApiResult<Map<String, Object>> chart(
            @Parameter(description = "图表类型：bar/line/pie") @RequestParam(required = false) String chartType) {
        return ApiResult.success(statisticsService.chartData(chartType));
    }

    /**
     * 自定义查询。
     *
     * @requirement(name="自定义查询", id="SRS-F-03-003")
     */
    @Operation(summary = "自定义查询")
    @PostMapping("/custom")
    public ApiResult<List<Map<String, Object>>> custom(@RequestBody(required = false) Map<String, Object> conditions) {
        return ApiResult.success(statisticsService.customQuery(conditions));
    }

    /**
     * 报表导出。
     *
     * @requirement(name="报表导出", id="SRS-F-03-004")
     */
    @Operation(summary = "报表导出")
    @PostMapping("/export")
    public ApiResult<Map<String, Object>> export(
            @Parameter(description = "导出格式：excel/pdf") @RequestParam(required = false) String format,
            @RequestBody(required = false) Map<String, Object> queryParam) {
        return ApiResult.success(statisticsService.export(format, queryParam));
    }

    /**
     * 数据台账生成。
     *
     * @requirement(name="数据台账生成", id="SRS-F-03-005")
     */
    @Operation(summary = "数据台账生成")
    @PostMapping("/ledger")
    public ApiResult<List<Map<String, Object>>> ledger(
            @Parameter(description = "业务类型") @RequestParam(required = false) String businessType,
            @Parameter(description = "开始日期") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) String endDate) {
        return ApiResult.success(statisticsService.ledger(businessType, startDate, endDate));
    }
}
