package com.competition.modules.biz.homework.controller;

import com.competition.common.result.PageResult;
import com.competition.common.result.Result;
import com.competition.common.util.SecurityUtil;
import com.competition.modules.biz.homework.dto.CreateSubmissionDto;
import com.competition.modules.biz.homework.entity.BizHomeworkSubmission;
import com.competition.modules.biz.homework.service.IHomeworkSubmissionService;
import com.competition.security.annotation.RequirePermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "作业提交")
@RestController
@RequestMapping("/homework/submissions")
@RequiredArgsConstructor
public class HomeworkSubmissionController {

    private final IHomeworkSubmissionService homeworkSubmissionService;

    @GetMapping
    @RequirePermission("homework:read")
    @Operation(summary = "查询作业提交列表")
    public Result<PageResult<Map<String, Object>>> findAll(
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) Long homeworkId,
            @RequestParam(required = false) String workNo,
            @RequestParam(required = false) String workName,
            @RequestParam(required = false) String studentAccount,
            @RequestParam(required = false) String studentName,
            @RequestParam(required = false) String status) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(homeworkSubmissionService.findAll(page, pageSize, tenantId, homeworkId,
                workNo, workName, studentAccount, studentName, status));
    }

    @GetMapping("/class-tree")
    @RequirePermission("homework:read")
    @Operation(summary = "获取班级树")
    public Result<List<Map<String, Object>>> getClassTree() {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(homeworkSubmissionService.getClassTree(tenantId));
    }

    @GetMapping("/my/{homeworkId}")
    @RequirePermission({"homework:read", "homework:student:read"})
    @Operation(summary = "查询我的作业提交")
    public Result<Map<String, Object>> findMySubmission(@PathVariable Long homeworkId) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(homeworkSubmissionService.findMySubmission(homeworkId, userId));
    }

    @PostMapping
    @RequirePermission({"homework:read", "homework:student:read"})
    @Operation(summary = "提交作业")
    public Result<BizHomeworkSubmission> create(@Valid @RequestBody CreateSubmissionDto dto) {
        Long studentId = SecurityUtil.getCurrentUserId();
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(homeworkSubmissionService.create(dto, studentId, tenantId));
    }

    @GetMapping("/{id}")
    @RequirePermission("homework:read")
    @Operation(summary = "查询作业提交详情")
    public Result<Map<String, Object>> findDetail(@PathVariable Long id) {
        return Result.success(homeworkSubmissionService.findDetail(id));
    }
}
