package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.presetcomment.CreatePresetCommentDTO;
import com.lesingle.creation.service.ContestPresetCommentService;
import com.lesingle.creation.vo.presetcomment.PresetCommentVO;
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/preset-comments")
@RequiredArgsConstructor
public class ContestPresetCommentController {

    private final ContestPresetCommentService presetCommentService;

    @PostMapping
    @Operation(summary = "创建预设评语")
    @PreAuthorize("hasAuthority('contest:preset-comment:create')")
    public Result<PresetCommentVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreatePresetCommentDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        PresetCommentVO result = presetCommentService.create(dto, tenantId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除预设评语")
    @PreAuthorize("hasAuthority('contest:preset-comment:delete')")
    public Result<Void> delete(@PathVariable Long id) {
        presetCommentService.delete(id);
        return Result.success(null);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取预设评语详情")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<PresetCommentVO> getDetail(@PathVariable Long id) {
        PresetCommentVO result = presetCommentService.getDetail(id);
        return Result.success(result);
    }

    @GetMapping("/contest/{contestId}")
    @Operation(summary = "查询活动的预设评语列表")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<List<PresetCommentVO>> listByContest(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long contestId) {
        Long tenantId = userPrincipal.getTenantId();
        List<PresetCommentVO> result = presetCommentService.listByContest(contestId, tenantId);
        return Result.success(result);
    }

    @GetMapping("/common")
    @Operation(summary = "查询通用评语列表")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<List<PresetCommentVO>> listCommon(
            @AuthenticationPrincipal UserPrincipal userPrincipal) {
        Long tenantId = userPrincipal.getTenantId();
        List<PresetCommentVO> result = presetCommentService.listCommon(tenantId);
        return Result.success(result);
    }
}
