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

import com.lesingle.common.result.Result;
import com.lesingle.common.util.SecurityUtil;
import com.lesingle.modules.biz.contest.dto.CreateTeamDto;
import com.lesingle.modules.biz.contest.entity.BizContestTeam;
import com.lesingle.modules.biz.contest.service.IContestTeamService;
import com.lesingle.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.List;
import java.util.Map;

@Tag(name = "团队管理")
@RestController
@RequestMapping("/contests/teams")
@RequiredArgsConstructor
public class ContestTeamController {

    private final IContestTeamService teamService;

    @PostMapping
    @RequirePermission("team:create")
    @Operation(summary = "创建团队")
    public Result<Map<String, Object>> create(@Valid @RequestBody CreateTeamDto dto) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        Long creatorId = SecurityUtil.getCurrentUserId();
        return Result.success(teamService.createTeam(dto, tenantId, creatorId));
    }

    @GetMapping("/contest/{contestId}")
    @RequirePermission("team:read")
    @Operation(summary = "查询活动下的团队列表")
    public Result<List<Map<String, Object>>> findByContest(@PathVariable Long contestId) {
        return Result.success(teamService.findByContest(contestId, SecurityUtil.getCurrentTenantId()));
    }

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

    @PatchMapping("/{id}")
    @RequirePermission("team:update")
    @Operation(summary = "更新团队")
    public Result<BizContestTeam> update(@PathVariable Long id, @RequestBody CreateTeamDto dto) {
        return Result.success(teamService.updateTeam(id, dto));
    }

    @PostMapping("/{id}/members")
    @RequirePermission("team:update")
    @Operation(summary = "添加团队成员")
    public Result<Void> addMember(@PathVariable Long id, @RequestBody Map<String, Object> body) {
        Long userId = ((Number) body.get("userId")).longValue();
        String role = (String) body.get("role");
        teamService.addMember(id, userId, role, SecurityUtil.getCurrentTenantId());
        return Result.success();
    }

    @DeleteMapping("/{id}/members/{userId}")
    @RequirePermission("team:update")
    @Operation(summary = "移除团队成员")
    public Result<Void> removeMember(@PathVariable Long id, @PathVariable Long userId) {
        teamService.removeMember(id, userId);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @RequirePermission("team:delete")
    @Operation(summary = "删除团队")
    public Result<Void> remove(@PathVariable Long id) {
        teamService.removeTeam(id);
        return Result.success();
    }
}
