package com.lesingle.edu.controller.teacher;

import com.lesingle.edu.common.response.Result;
import com.lesingle.edu.common.security.SecurityUtils;
import com.lesingle.edu.dto.request.CourseUsageQuery;
import com.lesingle.edu.dto.response.*;
import com.lesingle.edu.service.TeacherStatsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/**
 * 统计数据控制器（教师端）
 */
@RestController
@RequestMapping("/api/v1/teacher")
@RequiredArgsConstructor
@Tag(name = "教师端 - 统计数据", description = "教师端统计数据 API")
public class TeacherStatsController {

    private final TeacherStatsService teacherStatsService;

    @GetMapping("/dashboard")
    @Operation(summary = "获取教师端首页统计数据")
    public Result<TeacherDashboardResponse> getDashboard() {
        Long teacherId = SecurityUtils.getCurrentUserId();
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(teacherStatsService.getDashboard(teacherId, tenantId));
    }

    @GetMapping("/today-lessons")
    @Operation(summary = "获取今日课程")
    public Result<List<TeacherLessonVO>> getTodayLessons() {
        Long teacherId = SecurityUtils.getCurrentUserId();
        return Result.success(teacherStatsService.getTodayLessons(teacherId));
    }

    @GetMapping("/recommended-courses")
    @Operation(summary = "获取推荐课程")
    public Result<List<CoursePackageVO>> getRecommendedCourses() {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(teacherStatsService.getRecommendedCourses(tenantId));
    }

    @GetMapping("/weekly-stats")
    @Operation(summary = "获取本周统计")
    public Result<TeacherWeeklyStatsResponse> getWeeklyStats() {
        Long teacherId = SecurityUtils.getCurrentUserId();
        return Result.success(teacherStatsService.getWeeklyStats(teacherId));
    }

    @GetMapping("/lesson-trend")
    @Operation(summary = "获取授课趋势")
    public Result<List<TeacherLessonTrendVO>> getLessonTrend(
            @RequestParam(defaultValue = "6") int months) {
        Long teacherId = SecurityUtils.getCurrentUserId();
        return Result.success(teacherStatsService.getLessonTrend(teacherId, months));
    }

    @GetMapping("/course-usage-stats")
    @Operation(summary = "获取课程使用统计（增强版）")
    public Result<List<CourseUsageStatsVO>> getCourseUsageStats(
            @RequestParam(required = false) String periodType,
            @RequestParam(required = false) LocalDate startDate,
            @RequestParam(required = false) LocalDate endDate) {

        Long teacherId = SecurityUtils.getCurrentUserId();
        Long tenantId = SecurityUtils.getCurrentTenantId();

        CourseUsageQuery query = CourseUsageQuery.builder()
                .periodType(periodType)
                .startDate(startDate)
                .endDate(endDate)
                .build();

        return Result.success(teacherStatsService.getCourseUsageStats(tenantId, teacherId, query));
    }

    @GetMapping("/course-usage")
    @Operation(summary = "获取课程使用统计（旧版）")
    @Deprecated
    public Result<List<CourseUsageVO>> getCourseUsage() {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(teacherStatsService.getCourseUsage(tenantId));
    }
}
