package com.lesingle.edu.controller.school;

import com.lesingle.edu.common.response.PageResult;
import com.lesingle.edu.common.response.Result;
import com.lesingle.edu.common.security.SecurityUtils;
import com.lesingle.edu.dto.response.TaskCompletionDetailResponse;
import com.lesingle.edu.dto.response.TaskResponse;
import com.lesingle.edu.service.TaskService;
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.util.List;

/**
 * 学校端 - 阅读任务 API（只读）
 *
 * 重要说明：
 * - 学校端只能查看任务数据，不能创建/编辑/删除任务
 * - 支持多维度筛选：时间、班级、教师、类型、状态、完成率
 * - 可查看任务详情、完成情况列表、学生提交详情
 */
@Tag(name = "学校端 - 阅读任务（只读）", description = "学校端阅读任务查看 API")
@RestController
@RequestMapping("/api/v1/school/reading-tasks")
@RequiredArgsConstructor
public class SchoolTaskController {

    private final TaskService taskService;

    // =============== 任务列表（只读）==============

    @Operation(summary = "获取任务列表（支持多维度筛选）")
    @GetMapping
    public Result<PageResult<TaskResponse>> getTaskList(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) List<Long> classIds,
            @RequestParam(required = false) List<Long> teacherIds,
            @RequestParam(required = false) String dateType,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) String completionRate,
            @RequestParam(required = false) String sortBy,
            @RequestParam(required = false) String sortOrder) {

        Long tenantId = SecurityUtils.getCurrentTenantId();
        PageResult<TaskResponse> result = taskService.getSchoolTaskList(
                tenantId, pageNum, pageSize, keyword, type, status,
                classIds, teacherIds, dateType, startDate, endDate,
                completionRate, sortBy, sortOrder
        );
        return Result.success(result);
    }

    @Operation(summary = "获取任务详情")
    @GetMapping("/{taskId}")
    public Result<TaskResponse> getTaskDetail(@PathVariable Long taskId) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        TaskResponse response = taskService.getTaskDetailForSchool(taskId, tenantId);
        return Result.success(response);
    }

    // =============== 完成情况（只读）==============

    @Operation(summary = "获取任务完成情况列表")
    @GetMapping("/{taskId}/completions")
    public Result<PageResult<TaskCompletionDetailResponse>> getTaskCompletions(
            @PathVariable Long taskId,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String status) {

        Long tenantId = SecurityUtils.getCurrentTenantId();
        PageResult<TaskCompletionDetailResponse> result = taskService.getTaskCompletions(taskId, tenantId, pageNum, pageSize, status);
        return Result.success(result);
    }

    @Operation(summary = "获取学生提交详情")
    @GetMapping("/completions/{completionId}")
    public Result<TaskCompletionDetailResponse> getCompletionDetail(@PathVariable Long completionId) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        TaskCompletionDetailResponse response = taskService.getCompletionDetail(completionId, tenantId);
        return Result.success(response);
    }

    // =============== 统计数据（只读）==============

    @Operation(summary = "获取统计数据")
    @GetMapping("/statistics")
    public Result<Object> getStatistics(
            @RequestParam(required = false) String dateType,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {

        Long tenantId = SecurityUtils.getCurrentTenantId();
        // TODO: 实现统计数据接口
        return Result.success(null);
    }

}
