package com.ruoyi.xq.controller.app;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.xq.dto.app.common.IdsReq;
import com.ruoyi.xq.dto.app.user.*;
import com.ruoyi.xq.dto.app.user.vo.CurrentUserFullInfoVo;
import com.ruoyi.xq.dto.app.user.vo.CurrentUserInfoVo;
import com.ruoyi.xq.dto.app.user.vo.UserAuthInfoVo;
import com.ruoyi.xq.manager.CurrentUserManager;
import com.ruoyi.xq.service.UserExtendService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/current")
@Tag(name = "当前用户相关接口")
public class UserAppController {
    @Autowired
    private CurrentUserManager currentUserManager;
    @Autowired
    private UserExtendService userExtendService;

    @GetMapping("/user/query/homeInfo")
    @Operation(summary = "当前用户信息")
    @Log(title = "当前用户信息", businessType = BusinessType.OTHER, isPrintResponseData = false,isSaveDb = false)
    public R<CurrentUserInfoVo> homeInfo(){
        CurrentUserInfoVo vo = currentUserManager.currentBaseInfo();
        return R.ok(vo);
    }


    @GetMapping("/user/query/fullInfo")
    @Operation(summary = "当前用户的详细信息")
    @Log(title = "当前用户的详细信息", businessType = BusinessType.OTHER, isPrintResponseData = false,isSaveDb = false)
    public R<CurrentUserFullInfoVo> fullInfo(){
        CurrentUserFullInfoVo vo = currentUserManager.currentFullInfo();
        return R.ok(vo);
    }

    @GetMapping("/user/query/authInfo")
    @Operation(summary = "当前用户的认证信息")
    @Log(title = "当前用户的认证信息", businessType = BusinessType.OTHER,isPrintResponseData = false, isSaveDb = false)
    public R<UserAuthInfoVo> authInfo(){
        UserAuthInfoVo vo = currentUserManager.currentAuthInfo();
        return R.ok(vo);
    }

    @PostMapping("/user/update/base")
    @Operation(summary = "更新用户基本信息")
    @Log(title = "更新用户基本信息", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> updateBaseInfo(@RequestBody UpdateBaseInfoReq req){
        currentUserManager.updateBaseInfo(req);
        return R.ok();
    }

    @PostMapping("/user/update/auth")
    @Operation(summary = "更新用户认证信息")
    @Log(title = "更新用户认证信息", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> updateAuth(@RequestBody UpdateUserAuthInfoReq req){
        currentUserManager.updateInfoAuth(req);
        return R.ok();
    }

    @PostMapping("/user/update/info")
    @Operation(summary = "更新用户信息")
    @Log(title = "更新用户信息", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> updateInfo(@RequestBody UpdateUserFullInfoReq req){
        currentUserManager.updateInfo(req);
        return R.ok();
    }

    @PostMapping("/user/update/avatar")
    @Operation(summary = "更新用户头像信息")
    @Log(title = "更新用户信息", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> updateAvatarInfo(@RequestBody UpdateAvatarReq req){
        currentUserManager.updateAvatar(req.getAvatar());
        return R.ok();
    }

    @PostMapping("/user/update/remark")
    @Operation(summary = "更新用户备注信息")
    @Log(title = "更新用户备注信息", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> updateRemarkInfo(@RequestBody UpdateRemarkReq req){
        currentUserManager.updateRemark(req.getRemark());
        return R.ok();
    }

    @PostMapping("/user/add/picture")
    @Operation(summary = "新增用户照片")
    @Log(title = "新增用户照片", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> addUserPicture(@RequestBody AddPicturesReq req){
        currentUserManager.addUserPicture(req);
        return R.ok();
    }

    @PostMapping("/user/remove/picture")
    @Operation(summary = "批量删除用户照片")
    @Log(title = "批量删除用户照片", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> removeUserPicture(@RequestBody IdsReq req){
        List<Long> ids = Arrays.stream(req.getIds().split(","))
                .map(Long::valueOf)
                .collect(Collectors.toList());
        currentUserManager.removeUserPictures(ids);
        return R.ok();
    }

}
