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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.competition.common.result.Result;
import com.competition.modules.biz.contest.entity.BizContestAttachment;
import com.competition.modules.biz.contest.service.IContestAttachmentService;
import com.competition.security.annotation.RequirePermission;
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;

@Tag(name = "赛事附件")
@RestController
@RequestMapping("/contests/attachments")
@RequiredArgsConstructor
public class ContestAttachmentController {

    private final IContestAttachmentService attachmentService;

    @PostMapping
    @RequirePermission("contest:update")
    @Operation(summary = "上传附件")
    public Result<BizContestAttachment> create(@RequestBody BizContestAttachment attachment) {
        attachmentService.save(attachment);
        return Result.success(attachment);
    }

    @GetMapping("/contest/{contestId}")
    @RequirePermission("contest:read")
    @Operation(summary = "查询赛事下的附件列表")
    public Result<List<BizContestAttachment>> findByContest(@PathVariable Long contestId) {
        List<BizContestAttachment> list = attachmentService.list(
                new LambdaQueryWrapper<BizContestAttachment>()
                        .eq(BizContestAttachment::getContestId, contestId)
                        .orderByDesc(BizContestAttachment::getCreateTime));
        return Result.success(list);
    }

    @GetMapping("/{id}")
    @RequirePermission("contest:read")
    @Operation(summary = "查询附件详情")
    public Result<BizContestAttachment> findDetail(@PathVariable Long id) {
        return Result.success(attachmentService.getById(id));
    }

    @PatchMapping("/{id}")
    @RequirePermission("contest:update")
    @Operation(summary = "更新附件")
    public Result<Void> update(@PathVariable Long id, @RequestBody BizContestAttachment attachment) {
        attachment.setId(id);
        attachmentService.updateById(attachment);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @RequirePermission("contest:update")
    @Operation(summary = "删除附件")
    public Result<Void> remove(@PathVariable Long id) {
        attachmentService.removeById(id);
        return Result.success();
    }
}
