package com.lesingle.edu.controller.school;

import com.lesingle.edu.common.annotation.Log;
import com.lesingle.edu.common.enums.LogModule;
import com.lesingle.edu.common.enums.LogOperationType;
import com.lesingle.edu.common.annotation.RequireRole;
import com.lesingle.edu.common.enums.UserRole;
import com.lesingle.edu.common.security.SecurityUtils;
import com.lesingle.edu.service.SchoolExportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDate;

/**
 * 学校端 - 数据导出
 */
@Tag(name = "学校端 - 数据导出", description = "School Export APIs")
@RestController
@RequestMapping("/api/v1/school/export")
@RequiredArgsConstructor
@RequireRole(UserRole.SCHOOL)
@Slf4j
public class SchoolExportController {

    private final SchoolExportService schoolExportService;

    /**
     * 导出授课记录
     *
     * @param startDate 开始日期（yyyy-MM-dd）
     * @param endDate   结束日期（yyyy-MM-dd）
     * @param response  HTTP 响应
     */
    @GetMapping("/lessons")
    @Operation(summary = "导出授课记录")
    @Log(module = LogModule.EXPORT, type = LogOperationType.EXPORT, description = "导出授课记录")
    public void exportLessons(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            HttpServletResponse response) throws IOException {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        log.info("学校端导出授课记录，租户 ID: {}, 时间范围：{} ~ {}", tenantId, startDate, endDate);

        schoolExportService.exportLessons(
                tenantId,
                startDate != null && !startDate.isEmpty() ? LocalDate.parse(startDate) : null,
                endDate != null && !endDate.isEmpty() ? LocalDate.parse(endDate) : null,
                response
        );
    }

    /**
     * 导出教师绩效统计
     *
     * @param startDate 开始日期（yyyy-MM-dd）
     * @param endDate   结束日期（yyyy-MM-dd）
     * @param response  HTTP 响应
     */
    @GetMapping("/teacher-stats")
    @Operation(summary = "导出教师绩效统计")
    @Log(module = LogModule.EXPORT, type = LogOperationType.EXPORT, description = "导出教师绩效统计")
    public void exportTeacherStats(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            HttpServletResponse response) throws IOException {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        log.info("学校端导出教师绩效统计，租户 ID: {}, 时间范围：{} ~ {}", tenantId, startDate, endDate);

        schoolExportService.exportTeacherStats(
                tenantId,
                startDate != null && !startDate.isEmpty() ? LocalDate.parse(startDate) : null,
                endDate != null && !endDate.isEmpty() ? LocalDate.parse(endDate) : null,
                response
        );
    }

    /**
     * 导出学生统计
     *
     * @param classId  班级 ID（可选，为空时导出全校）
     * @param response HTTP 响应
     */
    @GetMapping("/student-stats")
    @Operation(summary = "导出学生统计")
    @Log(module = LogModule.EXPORT, type = LogOperationType.EXPORT, description = "导出学生统计")
    public void exportStudentStats(
            @RequestParam(required = false) Long classId,
            HttpServletResponse response) throws IOException {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        log.info("学校端导出学生统计，租户 ID: {}, 班级 ID: {}", tenantId, classId);

        schoolExportService.exportStudentStats(tenantId, classId, response);
    }
}
