package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.result.CreateResultDTO;
import com.lesingle.creation.service.ContestResultService;
import com.lesingle.creation.vo.result.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 竞赛结果管理控制器
 */
@Tag(name = "竞赛结果管理")
@RestController
@RequestMapping("/api/contests/results")
@RequiredArgsConstructor
public class ContestResultController {

    private final ContestResultService resultService;

    @PostMapping
    @Operation(summary = "创建竞赛结果")
    @PreAuthorize("hasAuthority('contest:result:create')")
    public Result<ResultVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreateResultDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long creatorId = userPrincipal.getUserId();
        ResultVO result = resultService.create(dto, creatorId);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新竞赛结果")
    @PreAuthorize("hasAuthority('contest:result:update')")
    public Result<ResultVO> update(
            @PathVariable Long id,
            @RequestBody @Validated CreateResultDTO dto) {
        ResultVO result = resultService.update(id, dto);
        return Result.success(result);
    }

    @PostMapping("/{id}/publish")
    @Operation(summary = "发布竞赛结果")
    @PreAuthorize("hasAuthority('contest:result:publish')")
    public Result<ResultVO> publish(@PathVariable Long id) {
        ResultVO result = resultService.publish(id);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除竞赛结果")
    @PreAuthorize("hasAuthority('contest:result:delete')")
    public Result<Void> delete(@PathVariable Long id) {
        resultService.delete(id);
        return Result.success(null);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取竞赛结果详情")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<ResultVO> getDetail(@PathVariable Long id) {
        ResultVO result = resultService.getDetail(id);
        return Result.success(result);
    }

    @GetMapping("/contest/{contestId}")
    @Operation(summary = "查询活动的竞赛结果列表")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<List<ResultVO>> listByContest(@PathVariable Long contestId) {
        List<ResultVO> result = resultService.listByContest(contestId);
        return Result.success(result);
    }

    @GetMapping("/contest/{contestId}/winners")
    @Operation(summary = "查询获奖结果列表")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<List<ResultVO>> listWinners(@PathVariable Long contestId) {
        List<ResultVO> result = resultService.listWinners(contestId);
        return Result.success(result);
    }
}
