package com.lesingle.edu.controller.school;

import com.lesingle.edu.common.annotation.RequireRole;
import com.lesingle.edu.common.enums.UserRole;
import com.lesingle.edu.common.response.Result;
import com.lesingle.edu.common.security.SecurityUtils;
import com.lesingle.edu.dto.response.CourseReportResponse;
import com.lesingle.edu.dto.response.ReportOverviewResponse;
import com.lesingle.edu.dto.response.StudentReportResponse;
import com.lesingle.edu.dto.response.TeacherReportResponse;
import com.lesingle.edu.service.SchoolReportService;
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;

/**
 * 学校端 - 数据报告
 */
@Tag(name = "学校端 - 数据报告", description = "School Report APIs")
@RestController
@RequestMapping("/api/v1/school/reports")
@RequiredArgsConstructor
@RequireRole(UserRole.SCHOOL)
public class SchoolReportController {

    private final SchoolReportService schoolReportService;

    @GetMapping("/overview")
    @Operation(summary = "获取报告概览")
    public Result<ReportOverviewResponse> getOverview(
            @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) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(schoolReportService.getOverview(tenantId, startDate, endDate));
    }

    @GetMapping("/teachers")
    @Operation(summary = "获取教师报告")
    public Result<List<TeacherReportResponse>> getTeacherReports(
            @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 = "返回数量限制，默认 50")
            @RequestParam(defaultValue = "50") int limit) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(schoolReportService.getTeacherReports(tenantId, startDate, endDate, limit));
    }

    @GetMapping("/courses")
    @Operation(summary = "获取课程报告")
    public Result<List<CourseReportResponse>> getCourseReports(
            @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 = "返回数量限制，默认 50")
            @RequestParam(defaultValue = "50") int limit) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(schoolReportService.getCourseReports(tenantId, startDate, endDate, limit));
    }

    @GetMapping("/students")
    @Operation(summary = "获取学生报告")
    public Result<List<StudentReportResponse>> getStudentReports(
            @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 = "返回数量限制，默认 50")
            @RequestParam(defaultValue = "50") int limit) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        return Result.success(schoolReportService.getStudentReports(tenantId, startDate, endDate, limit));
    }
}