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.CreateHomeworkDto;
import com.competition.modules.biz.homework.entity.BizHomework;
import com.competition.modules.biz.homework.service.IHomeworkService;
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/homeworks")
@RequiredArgsConstructor
public class HomeworkController {

    private final IHomeworkService homeworkService;

    @PostMapping
    @RequirePermission("homework:create")
    @Operation(summary = "创建作业")
    public Result<BizHomework> create(@Valid @RequestBody CreateHomeworkDto dto) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(homeworkService.create(dto, tenantId));
    }

    @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) String name,
            @RequestParam(required = false) String status) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(homeworkService.findAll(page, pageSize, tenantId, name, status));
    }

    @GetMapping("/my")
    @RequirePermission({"homework:read", "homework:student:read"})
    @Operation(summary = "查询我的作业列表")
    public Result<PageResult<Map<String, Object>>> findMyHomeworks(
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) String name) {
        Long userId = SecurityUtil.getCurrentUserId();
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(homeworkService.findMyHomeworks(page, pageSize, userId, tenantId, name));
    }

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

    @PatchMapping("/{id}")
    @RequirePermission("homework:update")
    @Operation(summary = "更新作业")
    public Result<BizHomework> update(@PathVariable Long id, @RequestBody CreateHomeworkDto dto) {
        return Result.success(homeworkService.update(id, dto));
    }

    @PostMapping("/{id}/publish")
    @RequirePermission("homework:update")
    @Operation(summary = "发布作业")
    public Result<Void> publish(@PathVariable Long id, @RequestBody(required = false) List<Integer> publishScope) {
        homeworkService.publish(id, publishScope);
        return Result.success();
    }

    @PostMapping("/{id}/unpublish")
    @RequirePermission("homework:update")
    @Operation(summary = "取消发布作业")
    public Result<Void> unpublish(@PathVariable Long id) {
        homeworkService.unpublish(id);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @RequirePermission("homework:delete")
    @Operation(summary = "删除作业")
    public Result<Void> remove(@PathVariable Long id) {
        homeworkService.remove(id);
        return Result.success();
    }
}
