package com.lesingle.edu.controller.school;

import com.lesingle.edu.common.response.Result;
import com.lesingle.edu.common.security.SecurityUtils;
import com.lesingle.edu.dto.response.TeacherActivityRankResponse;
import com.lesingle.edu.service.SchoolStatsService;
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.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 统计数据控制器（学校端）
 */
@RestController
@RequestMapping("/api/v1/school/stats")
@RequiredArgsConstructor
@Tag(name = "学校端 - 统计数据")
public class SchoolStatsController {

    private final SchoolStatsService schoolStatsService;

    @GetMapping
    @Operation(summary = "获取学校统计数据")
    public Result<Map<String, Object>> getSchoolStats() {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(schoolStatsService.getSchoolStats(tenantId));
    }

    @GetMapping("/teachers")
    @Operation(summary = "获取活跃教师排行")
    public Result<List<TeacherActivityRankResponse>> getActiveTeachers(
            @Parameter(description = "开始日期（YYYY-MM-DD），默认本月 1 号")
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @Parameter(description = "结束日期（YYYY-MM-DD），默认今天")
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            @Parameter(description = "返回数量限制，默认 10")
            @RequestParam(defaultValue = "10") int limit) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(schoolStatsService.getActiveTeachers(tenantId, startDate, endDate, limit));
    }

    @GetMapping("/courses")
    @Operation(summary = "获取课程使用统计")
    public Result<List<Map<String, Object>>> getCourseUsageStats(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(schoolStatsService.getCourseUsageStats(tenantId, startDate, endDate));
    }

    @GetMapping("/lesson-trend")
    @Operation(summary = "获取授课趋势")
    public Result<List<Map<String, Object>>> getLessonTrend(
            @Parameter(description = "开始日期（YYYY-MM-DD），默认 7 天前")
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @Parameter(description = "结束日期（YYYY-MM-DD），默认今天")
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(schoolStatsService.getLessonTrend(tenantId, startDate, endDate));
    }

    @GetMapping("/course-distribution")
    @Operation(summary = "获取课程分布")
    public Result<List<Map<String, Object>>> getCourseDistribution() {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(schoolStatsService.getCourseDistribution(tenantId));
    }
}
