package com.seer.teach.user.app.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.DecryptionAnnotation;
import com.seer.teach.common.annotation.EncryptionAnnotation;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.response.OssImageUploadResp;
import com.seer.teach.common.service.CommonFileService;
import com.seer.teach.user.app.user.controller.req.JudgeAreaReq;
import com.seer.teach.user.app.user.controller.req.AppUserInfoReq;
import com.seer.teach.user.app.user.controller.req.UpdateUserInfoReq;
import com.seer.teach.user.app.user.controller.req.UploadEnAvatarReq;
import com.seer.teach.user.app.user.controller.resp.AppGetUserInfoResp;
import com.seer.teach.user.app.user.controller.resp.AppProvinceCityResp;
import com.seer.teach.user.app.user.controller.resp.AppUserInfoResp;
import com.seer.teach.user.app.user.service.AppUserInfoService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

@EncryptionAnnotation
@DecryptionAnnotation
@RestController
@Tag(name = "App 用户接口")
@Validated
@LogPrint
@SaCheckLogin(type = StpUtil.TYPE)
@RequiredArgsConstructor
@RequestMapping("/app")
public class AppUserInfoController {

    private final AppUserInfoService appUserInfoService;

    private final CommonFileService commonFileService;

    @Operation(summary = "获取用户信息")
    @GetMapping("/user-detail")
    @SaCheckPermission("app:user:detail")
    public ResultBean<AppUserInfoResp> getUserDetail() {
        return ResultBean.success(appUserInfoService.getUserDetail());
    }

    /**
     * 判断省市
     *
     * @param judgeAreaReq
     * @return
     */
    @Operation(summary = "判断省市")
    @PostMapping("/judgeArea")
    @SaCheckLogin(type = StpUtil.TYPE)
    @SaCheckPermission("app:user:area:judge")
    public ResultBean<Integer> judgeArea(@RequestBody @Validated JudgeAreaReq judgeAreaReq) {
        return ResultBean.success(appUserInfoService.judgeArea(judgeAreaReq));
    }

    /**
     * 更新用户的信息，仅更新头像和昵称
     */
    @Operation(summary = "更新用户的信息", description = "仅更新头像和昵称")
    @PutMapping("/update-user-simple")
    @SaCheckLogin(type = StpUtil.TYPE)
    @SaCheckPermission("app:user:update:simple")
    public ResultBean<Boolean> updateUserSimpleInfo(@RequestBody @Validated AppUserInfoReq appUserInfoReq) {
        return ResultBean.success(appUserInfoService.updateUserSimpleInfo(appUserInfoReq));
    }

    @PostMapping("/uploadAvatarImage")
    @Operation(summary = "上传头像")
    @SaCheckPermission("app:user:avatar:upload")
    public ResultBean<OssImageUploadResp> uploadAvatarImage(@RequestParam("file") @NotNull(message = "文件不能为空") MultipartFile file) throws IOException {
        OssImageUploadResp result = commonFileService.uploadImage(StpUtil.getLoginIdAsInt(), file);
        return ResultBean.success(result);
    }

    @PostMapping("/uploadEnAvatar")
    @Operation(summary = "上传头像-编码")
    @SaCheckPermission("app:user:avatar:upload")
    public ResultBean<OssImageUploadResp> uploadAvatarImage(@RequestBody @Validated UploadEnAvatarReq params) throws IOException {
        OssImageUploadResp result = commonFileService.uploadEnImage(StpUtil.getLoginIdAsInt(), params.getImageBase64());
        return ResultBean.success(result);
    }

    @PostMapping("/updateUserPad")
    @Operation(summary = "更新用户信息")
    @SaCheckPermission("app:user:update:pad")
    public ResultBean<Boolean> updateUser(@RequestBody @Validated UpdateUserInfoReq params){
       return ResultBean.success(appUserInfoService.updateUserInfo(params));
    }

    @PostMapping("/getUserInfo")
    @Operation(summary = "获取用户信息")
    @SaCheckPermission("app:user:detail")
    public ResultBean<AppGetUserInfoResp> getUserInfo(){
        return ResultBean.success(appUserInfoService.getUserInfo());
    }

    @PostMapping("/getProvinceAndCity")
    @Operation(summary = "获取省份城市")
    @SaCheckPermission("app:user:area:get")
    public ResultBean<List<AppProvinceCityResp>> getProvinceAndCity(){
        return ResultBean.success(appUserInfoService.getProvinceAndCity());
    }

}