package com.lesingle.modules.biz.contest.controller;

import com.lesingle.common.result.PageResult;
import com.lesingle.common.result.Result;
import com.lesingle.common.util.SecurityUtil;
import com.lesingle.modules.biz.contest.dto.QueryWorkDto;
import com.lesingle.modules.biz.contest.dto.SubmitWorkDto;
import com.lesingle.modules.biz.contest.service.IContestWorkService;
import com.lesingle.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("/contests/works")
@RequiredArgsConstructor
public class ContestWorkController {

    private final IContestWorkService workService;

    @PostMapping("/submit")
    @RequirePermission("work:submit")
    @Operation(summary = "提交作品")
    public Result<Map<String, Object>> submit(@Valid @RequestBody SubmitWorkDto dto) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        Long submitterId = SecurityUtil.getCurrentUserId();
        return Result.success(workService.submitWork(dto, tenantId, submitterId));
    }

    @GetMapping("/stats")
    @RequirePermission("work:read")
    @Operation(summary = "获取作品统计")
    public Result<Map<String, Object>> getStats(@RequestParam(required = false) Long contestId) {
        return Result.success(
                workService.getStats(contestId, SecurityUtil.getCurrentTenantId(), SecurityUtil.isSuperAdmin())
        );
    }

    @GetMapping
    @RequirePermission("work:read")
    @Operation(summary = "查询作品列表")
    public Result<PageResult<Map<String, Object>>> findAll(QueryWorkDto dto) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        boolean isSuperTenant = SecurityUtil.isSuperAdmin();
        return Result.success(workService.findAll(dto, tenantId, isSuperTenant));
    }

    @GetMapping("/guided")
    @RequirePermission("activity:read")
    @Operation(summary = "查询辅导作品")
    public Result<PageResult<Map<String, Object>>> getGuidedWorks(
            @RequestParam(required = false) Long contestId,
            @RequestParam(required = false) String workNo,
            @RequestParam(required = false) String playerName,
            @RequestParam(required = false) String accountNo,
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "10") Long pageSize) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(workService.getGuidedWorks(contestId, workNo, playerName, accountNo, page, pageSize, userId));
    }

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

    @GetMapping("/registration/{registrationId}/versions")
    @RequirePermission("work:read")
    @Operation(summary = "查询作品版本历史")
    public Result<List<Map<String, Object>>> getWorkVersions(@PathVariable Long registrationId) {
        return Result.success(workService.getWorkVersions(registrationId));
    }

    @DeleteMapping("/{id}")
    @RequirePermission("work:update")
    @Operation(summary = "删除作品")
    public Result<Void> remove(@PathVariable Long id) {
        workService.removeWork(id);
        return Result.success();
    }
}
