package com.lesingle.creation.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.contest.*;
import com.lesingle.creation.service.ContestService;
import com.lesingle.creation.vo.contest.ContestDetailVO;
import com.lesingle.creation.vo.contest.ContestListVO;
import com.lesingle.creation.vo.contest.ContestStatsVO;
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.*;

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

    private final ContestService contestService;

    @PostMapping
    @Operation(summary = "创建竞赛")
    @PreAuthorize("hasAuthority('contest:create')")
    public Result<ContestDetailVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreateContestDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        ContestDetailVO result = contestService.create(dto, tenantId);
        return Result.success(result);
    }

    @GetMapping("/stats")
    @Operation(summary = "竞赛统计")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<ContestStatsVO> getStats() {
        ContestStatsVO result = contestService.getStats();
        return Result.success(result);
    }

    @GetMapping
    @Operation(summary = "竞赛列表")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<Page<ContestListVO>> pageList(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            ContestQueryDTO queryDTO) {
        Long tenantId = userPrincipal.getTenantId();
        Page<ContestListVO> result = contestService.pageList(queryDTO, tenantId);
        return Result.success(result);
    }

    @GetMapping("/my-contests")
    @Operation(summary = "我参与的活动列表")
    public Result<Page<ContestListVO>> myContests(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            ContestQueryDTO queryDTO) {
        Long userId = userPrincipal.getUserId();
        Long tenantId = userPrincipal.getTenantId();
        Page<ContestListVO> result = contestService.myContests(queryDTO, userId, tenantId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "竞赛详情")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<ContestDetailVO> detail(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        ContestDetailVO result = contestService.detail(id, tenantId);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新竞赛")
    @PreAuthorize("hasAuthority('contest:update')")
    public Result<ContestDetailVO> update(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Validated UpdateContestDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        ContestDetailVO result = contestService.update(id, dto, tenantId);
        return Result.success(result);
    }

    @PatchMapping("/{id}/publish")
    @Operation(summary = "发布/取消发布竞赛")
    @PreAuthorize("hasAuthority('contest:publish')")
    public Result<ContestDetailVO> publish(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Validated PublishContestDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        ContestDetailVO result = contestService.publish(id, dto.getContestState(), tenantId);
        return Result.success(result);
    }

    @PatchMapping("/{id}/finish")
    @Operation(summary = "标记竞赛为完结")
    @PreAuthorize("hasAuthority('contest:update')")
    public Result<ContestDetailVO> finish(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        ContestDetailVO result = contestService.finish(id, tenantId);
        return Result.success(result);
    }

    @PatchMapping("/{id}/reopen")
    @Operation(summary = "重新开启竞赛")
    @PreAuthorize("hasAuthority('contest:update')")
    public Result<ContestDetailVO> reopen(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        ContestDetailVO result = contestService.reopen(id, tenantId);
        return Result.success(result);
    }

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