package com.ruoyi.web.app;

import com.ruoyi.bst.team.domain.Team;
import com.ruoyi.bst.team.domain.TeamQuery;
import com.ruoyi.bst.team.domain.TeamVO;
import com.ruoyi.bst.team.domain.dto.InvitedDTO;
import com.ruoyi.bst.team.service.TeamConverter;
import com.ruoyi.bst.team.service.TeamService;
import com.ruoyi.bst.team.service.TeamValidator;
import com.ruoyi.bst.teamUser.domain.TeamUser;
import com.ruoyi.bst.teamUser.domain.TeamUserQuery;
import com.ruoyi.bst.teamUser.domain.TeamUserVO;
import com.ruoyi.bst.teamUser.domain.enums.RoleType;
import com.ruoyi.bst.teamUser.service.TeamUserConverter;
import com.ruoyi.bst.teamUser.service.TeamUserService;
import com.ruoyi.bst.teamUser.service.TeamUserValidator;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.uuid.IdUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.cache.CacheKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/app/team")
public class AppTeamController extends BaseController {

    @Autowired
    private TeamService teamService;

    @Autowired
    private TeamConverter teamConverter;

    @Autowired
    private TeamUserService teamUserService;

    @Autowired
    private TeamUserValidator teamUserValidator;



    @Autowired
    private RedisCache redisCache;
    @Autowired
    private TeamValidator teamValidator;

    @ApiOperation("创建队伍")
    @PostMapping("/createTeam")
    public AjaxResult list(@RequestBody Team team) {
        if (!teamUserValidator.canJoinTeam(getUserId())) {
            return AjaxResult.error("您有正在进行的拼桌,请先结束后再进行拼桌");
        }
        Team po = teamConverter.toPoByCreate(team);
        return AjaxResult.success(teamService.createTeam(po, getUserId()));
    }

    @ApiOperation("加入队伍")
    @PostMapping("/joinTeam")
    public AjaxResult joinTeam(@RequestParam Long teamId, @RequestParam(required = false) String inviteCode) {
        if (!teamUserValidator.canJoinTeam(getUserId())) {
            return AjaxResult.error("您有正在进行的拼桌,请先结束后再进行拼桌");
        }
        if (!teamValidator.canAddUser(teamId)) {
            return AjaxResult.error("当前拼桌已解散,无法加入");
        }
        if (inviteCode != null && !inviteCode.isEmpty()) {
            String verifyKey = CacheConstants.TABLE_SHARING_INVITATION_KEY + inviteCode;
            InvitedDTO dto = redisCache.getCacheObject(verifyKey);
            if (dto == null) {
                return AjaxResult.error("当前验证码无效,请重新获取验证码");
            }
            if (!dto.getTeamId().equals(teamId)) {
                return AjaxResult.error("该验证码与所申请加入的拼桌验证码不符,请输入正确的验证码");
            }
            return AjaxResult.success(teamService.invitedTeam(dto.getTeamId(), getUserId(), dto.getInvitedId()));
        }
        return AjaxResult.success(teamService.joinTeam(teamId, getUserId()));
    }


    @ApiOperation("退出队伍")
    @PutMapping("/exitTeam")
    public AjaxResult exitTeam(@RequestParam Long teamId) {
        return AjaxResult.success(teamService.exitTeam(teamId, getUserId()));
    }

    @ApiOperation("解散队伍")
    @PutMapping("/disTeam")
    public AjaxResult disTeam(@RequestParam Long teamId) {
        // 权限校验
        TeamUserQuery query = new TeamUserQuery();
        query.setTeamId(teamId);
        query.setRole(RoleType.CREATOR.getCode());
        TeamUserVO vo = teamUserService.selectOne(query);
        if (!vo.getUserId().equals(getUserId())) {
            return AjaxResult.error("该拼桌不属于您,您无权解散");
        }
        return AjaxResult.success(teamService.disTeam(teamId));
    }

    @ApiOperation("生成邀请码")
    @PostMapping("/invCode")
    public AjaxResult invCode(@RequestBody InvitedDTO dto) {
        String uuid = IdUtils.simpleUUID();
        String verifyKey = CacheConstants.TABLE_SHARING_INVITATION_KEY + uuid;
        redisCache.setCacheObject(verifyKey, dto, 15, TimeUnit.MINUTES);
        return AjaxResult.success(uuid);
    }

    @ApiOperation("获取拼桌成员信息")
    @GetMapping("/getTeammate")
    public AjaxResult getTeammate(@RequestParam Long teamId) {
        return AjaxResult.success(teamService.getTeammate(teamId));
    }


}
