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

import com.competition.common.result.PageResult;
import com.competition.common.result.Result;
import com.competition.common.util.SecurityUtil;
import com.competition.modules.biz.contest.dto.CreateContestDto;
import com.competition.modules.biz.contest.dto.QueryContestDto;
import com.competition.modules.biz.contest.entity.BizContest;
import com.competition.modules.biz.contest.service.IContestService;
import com.competition.modules.sys.service.ISysTenantService;
import com.competition.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.Map;

@Tag(name = "赛事管理")
@RestController
@RequestMapping("/contests")
@RequiredArgsConstructor
public class ContestController {

    private final IContestService contestService;
    private final ISysTenantService tenantService;

    @PostMapping
    @RequirePermission("contest:create")
    @Operation(summary = "创建赛事")
    public Result<BizContest> create(@Valid @RequestBody CreateContestDto dto) {
        return Result.success(contestService.createContest(dto, SecurityUtil.getCurrentUserId()));
    }

    @GetMapping("/stats")
    @RequirePermission("contest:read")
    @Operation(summary = "获取赛事统计")
    public Result<Map<String, Object>> getStats() {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        boolean isSuperTenant = tenantService.isSuperTenant(tenantId);
        return Result.success(contestService.getStats(tenantId, isSuperTenant));
    }

    @GetMapping("/dashboard")
    @RequirePermission("contest:read")
    @Operation(summary = "获取赛事看板")
    public Result<Map<String, Object>> getDashboard() {
        return Result.success(contestService.getDashboard(SecurityUtil.getCurrentTenantId()));
    }

    @GetMapping
    @RequirePermission("contest:read")
    @Operation(summary = "查询赛事列表")
    public Result<PageResult<Map<String, Object>>> findAll(QueryContestDto dto) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        boolean isSuperTenant = tenantService.isSuperTenant(tenantId);
        return Result.success(contestService.findAll(dto, tenantId, isSuperTenant));
    }

    @GetMapping("/my-contests")
    @RequirePermission({"contest:read", "contest:activity:read"})
    @Operation(summary = "获取我的赛事")
    public Result<PageResult<Map<String, Object>>> getMyContests(QueryContestDto dto) {
        Long userId = SecurityUtil.getCurrentUserId();
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(contestService.getMyContests(dto, userId, tenantId));
    }

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

    @PatchMapping("/{id}")
    @RequirePermission("contest:update")
    @Operation(summary = "更新赛事")
    public Result<BizContest> update(@PathVariable Long id, @RequestBody CreateContestDto dto) {
        return Result.success(contestService.updateContest(id, dto));
    }

    @PatchMapping("/{id}/publish")
    @RequirePermission("contest:publish")
    @Operation(summary = "发布/撤回赛事")
    public Result<Void> publish(@PathVariable Long id, @RequestBody Map<String, String> body) {
        contestService.publishContest(id, body.get("contestState"));
        return Result.success();
    }

    @PatchMapping("/{id}/finish")
    @RequirePermission("contest:update")
    @Operation(summary = "结束赛事")
    public Result<Void> finish(@PathVariable Long id) {
        contestService.finishContest(id);
        return Result.success();
    }

    @PatchMapping("/{id}/reopen")
    @RequirePermission("contest:update")
    @Operation(summary = "重新开放赛事")
    public Result<Void> reopen(@PathVariable Long id) {
        contestService.reopenContest(id);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @RequirePermission("contest:delete")
    @Operation(summary = "删除赛事")
    public Result<Void> remove(@PathVariable Long id) {
        contestService.removeContest(id);
        return Result.success();
    }
}
