package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.team.*;
import com.lesingle.creation.service.ContestTeamService;
import com.lesingle.creation.vo.team.TeamVO;
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/teams")
@RequiredArgsConstructor
public class ContestTeamController {

    private final ContestTeamService teamService;

    @PostMapping
    @Operation(summary = "创建团队")
    @PreAuthorize("hasAuthority('contest:team:create')")
    public Result<TeamVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreateTeamDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long creatorId = userPrincipal.getUserId();
        TeamVO result = teamService.create(dto, tenantId, creatorId);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新团队")
    @PreAuthorize("hasAuthority('contest:team:update')")
    public Result<TeamVO> update(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Validated UpdateTeamDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        TeamVO result = teamService.update(id, dto, tenantId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除团队")
    @PreAuthorize("hasAuthority('contest:team:delete')")
    public Result<Void> delete(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        teamService.delete(id, tenantId);
        return Result.success(null);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取团队详情")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<TeamVO> getDetail(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        TeamVO result = teamService.getDetail(id, tenantId);
        return Result.success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "查询团队列表")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<List<TeamVO>> list(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestParam(required = false) Long contestId) {
        Long tenantId = userPrincipal.getTenantId();
        List<TeamVO> result = teamService.list(contestId, tenantId);
        return Result.success(result);
    }

    @GetMapping("/contest/{contestId}")
    @Operation(summary = "按竞赛查询团队列表")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<List<TeamVO>> listByContest(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long contestId) {
        Long tenantId = userPrincipal.getTenantId();
        List<TeamVO> result = teamService.listByContest(contestId, tenantId);
        return Result.success(result);
    }

    @GetMapping("/my/{contestId}")
    @Operation(summary = "获取用户在某活动中的团队")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<TeamVO> getMyTeam(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long contestId) {
        Long userId = userPrincipal.getUserId();
        Long tenantId = userPrincipal.getTenantId();
        TeamVO result = teamService.getMyTeam(contestId, userId, tenantId);
        return result != null ? Result.success(result) : Result.success(null);
    }

    @PostMapping("/{id}/members")
    @Operation(summary = "邀请成员")
    @PreAuthorize("hasAuthority('contest:team:update')")
    public Result<Void> inviteMember(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Validated InviteMemberDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long creatorId = userPrincipal.getUserId();
        teamService.inviteMember(id, dto, tenantId, creatorId);
        return Result.success(null);
    }

    @DeleteMapping("/{id}/members/{userId}")
    @Operation(summary = "移除成员")
    @PreAuthorize("hasAuthority('contest:team:update')")
    public Result<Void> removeMember(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @PathVariable Long userId) {
        Long tenantId = userPrincipal.getTenantId();
        teamService.removeMember(id, userId, tenantId);
        return Result.success(null);
    }
}
