package cn.sikey.mcdisk.controller.app.familycloud;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.sikey.framework.common.pojo.CommonResult;
import cn.sikey.mcdisk.controller.app.familycloud.vo.AppOauthReqVO;
import cn.sikey.mcdisk.controller.app.familycloud.vo.ConfirmInvitationReqVO;
import cn.sikey.mcdisk.controller.app.familycloud.vo.QueryUserOauthRespVO;
import cn.sikey.mcdisk.enums.RateLimit;
import cn.sikey.mcdisk.pojo.McdiskResult;
import cn.sikey.mcdisk.service.AppFamilyCloudService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Author: nelson
 * @Date: 2025/5/29
 * @Description: 家庭云
 */
@RestController
@RequestMapping("/mcdisk/app/familyCloud")
@Slf4j
public class AppFamilyCloudController {

    @Resource
    private AppFamilyCloudService appFamilyCloudService;

    /**
     * 邀请用户加入家庭云（链接模式）
     *
     * @param inviteFamilyMemberReqVO 邀请用户加入家庭云（链接模式）
     * @return
     */
    /*@PostMapping("/inviteFamilyMember")
    public CommonResult inviteFamilyMember(@Validated @RequestBody InviteFamilyMemberReqVO inviteFamilyMemberReqVO) {
        McdiskResult mcdiskResult = appFamilyCloudService.inviteFamilyMember(inviteFamilyMemberReqVO.getCloudId(), inviteFamilyMemberReqVO.getChannelType());
        return CommonResult.success(mcdiskResult.getData());
    }*/

    /**
     * 查询用户认证
     *
     * @param appOauthReqVO 查询用户认证
     * @return
     */
    @RateLimit(key = "queryUserAuthorization", type = RateLimit.LimitType.GLOBAL)
    @GetMapping("/queryUserAuthorization")
    public CommonResult<QueryUserOauthRespVO> queryUserOauth(@Validated AppOauthReqVO appOauthReqVO) {
        return CommonResult.success(appFamilyCloudService.queryUserOauth(appOauthReqVO));
    }

    /**
     * 用户认证
     *
     * @param appOauthReqVO app认证
     * @return
     */
    @RateLimit(key = "authorization", type = RateLimit.LimitType.GLOBAL)
    @PostMapping("/authorization")
    public CommonResult appAuthorization(@Validated @RequestBody AppOauthReqVO appOauthReqVO) {
        McdiskResult mcdiskResult = appFamilyCloudService.oauth(appOauthReqVO);
        String invitationCode = StringUtils.EMPTY;
        if(Objects.nonNull(mcdiskResult) && Objects.nonNull(mcdiskResult.getData())) {
            invitationCode = JSONUtil.parseObj(JSONUtil.toJsonStr(mcdiskResult.getData())).getStr("invitationCode");
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("invitationCode", invitationCode);
        return CommonResult.success(jsonObject);
    }

    /**
     * 确认邀请
     *
     * @param confirmInvitationReqVO 确认邀请
     * @return
     */
    @RateLimit(key = "confirmInvitation", type = RateLimit.LimitType.GLOBAL)
    @PostMapping("/confirmInvitation")
    public CommonResult<QueryUserOauthRespVO> confirmInvitation(@Validated @RequestBody ConfirmInvitationReqVO confirmInvitationReqVO) {
        QueryUserOauthRespVO queryUserOauthRespVO = appFamilyCloudService.confirmInvitation(confirmInvitationReqVO);
        return CommonResult.success(queryUserOauthRespVO);
    }

    /**
     * 查询家庭云相册内容列表
     *
     * @param queryContentInfoV3ReqVO 查询家庭云相册内容列表
     * @return
     */
    /*@GetMapping("/queryContentInfoV3")
    public CommonResult queryContentInfoV3(@Validated QueryContentInfoV3ReqVO queryContentInfoV3ReqVO) {
        McdiskResult mcdiskResult = appFamilyCloudService.queryContentInfoV3(queryContentInfoV3ReqVO);
        return CommonResult.success(mcdiskResult.getData());
    }*/

    /**
     * 下载家庭云文件
     *
     * @param getFileDownLoadURLReqVO 下载家庭云文件
     * @return
     */
    /*@PostMapping("/getFileDownLoadURL")
    public CommonResult getFileDownLoadURL(@Validated @RequestBody GetFileDownLoadURLReqVO getFileDownLoadURLReqVO) {
        McdiskResult mcdiskResult = appFamilyCloudService.getFileDownLoadURL(getFileDownLoadURLReqVO);
        return CommonResult.success(mcdiskResult.getData());
    }*/

}
