package com.zhongshu.card.server.core.controller.user;

import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.helper.AuthHelper;
import com.github.microservice.auth.security.type.AuthType;
import com.zhongshu.card.client.model.base.AuthTypeParam;
import com.zhongshu.card.client.model.base.UserIdModel;
import com.zhongshu.card.client.model.org.*;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.service.org.OrganizationService;
import com.zhongshu.card.client.service.org.UserAccountService;
import com.zhongshu.card.client.type.UserState;
import com.zhongshu.card.server.core.service.org.IndexService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author TRX
 * @date 2024/6/4
 */
@RestController
@RequestMapping("userInfo")
@Tag(name = "登录用户相关")
public class UserInfoController {

    @Autowired
    IndexService indexService;

    @Autowired
    OrganizationService organizationService;

    @Autowired
    AuthHelper authHelper;

    @Autowired
    UserAccountService userAccountService;

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到当前用户信息（基本信息）", description = "得到当前用户信息（基本信息）")
    @RequestMapping(value = {"getCurrentDetail"}, method = {RequestMethod.POST})
    public ResultContent<UserCountDetailsModel> getCurrentDetail(@RequestBody AuthTypeParam param) {
        return userAccountService.getCurrentDetail(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到当前用户信息（基本信息）", description = "得到当前用户信息（基本信息）")
    @RequestMapping(value = {"getBusCurrentDetail"}, method = {RequestMethod.POST})
    public ResultContent<UserCountDetailsModel> getBusCurrentDetail(@RequestBody AuthTypeParam param) {
        return userAccountService.getBusCurrentDetail(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到当前用户在当前机构的信息", description = "得到当前用户在当前机构的信息")
    @RequestMapping(value = {"getCurrentUserOrg"}, method = {RequestMethod.GET})
    public ResultContent<OrganizationUserModel> getCurrentUserOrg() {
        return userAccountService.getCurrentUserOrg();
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到当前用户信息+所有指定的机构数据", description = "得到当前用户信息 + 所有指定的机构数据")
    @RequestMapping(value = {"getCurrentDetailAndOrgs"}, method = {RequestMethod.POST})
    public ResultContent<UserCountDetailsModel> getCurrentDetailAndOrgs(@RequestBody AuthTypeParam param) {
        if (param.getLoginFromType() == null) {
            return ResultContent.buildFail("loginFromType 不能为空");
        }
        return userAccountService.getCurrentDetailAndOrgs(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "修改当前用户密码", description = "修改当前用户密码")
    @RequestMapping(value = {"updatePassWord"}, method = {RequestMethod.POST})
    public ResultContent<UserCountDetailsModel> updatePassWord(@RequestBody PassWordModel param) {
        param.setUserId(authHelper.getCurrentUser().getUserId());
        return userAccountService.updatePassWord(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "重置用户密码", description = "重置用户密码（重置为默认密码）")
    @RequestMapping(value = {"resetPassWord"}, method = {RequestMethod.POST})
    public ResultContent resetPassWord(@RequestBody UserIdModel param) {
        Assert.hasText(param.getUserId(), "userId不能为空");
        return userAccountService.resetPassWord(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "修改用户头像", description = "修改用户头像")
    @RequestMapping(value = {"updateUserHead"}, method = {RequestMethod.GET})
    public ResultContent<UserCountDetailsModel> updateUserHead(@Parameter(name = "url", description = "头像url", example = "")
    @RequestParam("url") String url) {
        return userAccountService.updateUserHead(url);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "编辑当前用户的基本信息", description = "编辑当前用户的基本信息")
    @RequestMapping(value = {"updateUserInfo"}, method = {RequestMethod.POST})
    public ResultContent<UserCountDetailsModel> updateUserInfo(@RequestBody UserCountUpdateParam param) {
        Assert.hasText(param.getName(), "name不能为空");
        return userAccountService.updateUserInfo(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "查询用户加入的平台数据列表", description = "查询用户加入的 平台数据列表")
    @RequestMapping(value = {"getUserPlatform"}, method = {RequestMethod.GET})
    public ResultContent<List<OrganizationModel>> getUserPlatform() {
        OrganizationUserSearch param = new OrganizationUserSearch();
        param.setAuthType(AuthType.Platform);
        param.setState(UserState.Normal);
        return organizationService.getUserOrgList(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "查询用户加入的 项目数据列表", description = "查询用户加入的 项目数据列表")
    @RequestMapping(value = {"getUserProject"}, method = {RequestMethod.GET})
    public ResultContent<List<OrganizationModel>> getUserProject() {
        OrganizationUserSearch param = new OrganizationUserSearch();
        param.setAuthType(AuthType.Project);
        param.setState(UserState.Normal);
        return organizationService.getUserOrgList(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "查询用户加入的 学校数据列表", description = "查询用户加入的 学校数据列表")
    @RequestMapping(value = {"getUserSchool"}, method = {RequestMethod.GET})
    public ResultContent<List<OrganizationModel>> getUserSchool() {
        OrganizationUserSearch param = new OrganizationUserSearch();
        param.setAuthType(AuthType.School);
        param.setState(UserState.Normal);
        return organizationService.getUserOrgList(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "查询用户加入的 商家数据列表", description = "查询用户加入的 商家数据列表")
    @RequestMapping(value = {"getUserShop"}, method = {RequestMethod.GET})
    public ResultContent<List<OrganizationModel>> getUserShop() {
        OrganizationUserSearch param = new OrganizationUserSearch();
        param.setAuthType(AuthType.BusinessMain);
        param.setState(UserState.Normal);
        return organizationService.getUserOrgList(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "查询用户加入的 企业数据列表", description = "查询用户加入的 企业数据列表")
    @RequestMapping(value = {"getUserEnterprise"}, method = {RequestMethod.GET})
    public ResultContent<List<OrganizationModel>> getUserEnterprise() {
        OrganizationUserSearch param = new OrganizationUserSearch();
        param.setAuthType(AuthType.Enterprise);
        param.setState(UserState.Normal);
        return organizationService.getUserOrgList(param);
    }

    //------------------------------------feign start -----------------------------

    @Operation(hidden = true, summary = "feign得到用户详情", description = "feign得到用户详情")
    @RequestMapping(value = {"manager/getUserDetailByUserId"}, method = {RequestMethod.GET})
    public ResultContent<UserCountModel> getUserDetailByUserId(
            @Parameter(name = "userId", description = "用户UserId", example = "")
            @RequestParam("userId") String userId) {
        return userAccountService.getUserDetail(userId);
    }


}
