package com.ruoyi.web.controller.client;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.ShopUser;
import com.ruoyi.common.core.domain.entity.ShopUserResq;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.config.AliConfig;
import com.ruoyi.system.service.AliConfigService;
import com.ruoyi.system.service.IShopUserService;
import com.ruoyi.system.service.ShopUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/client")
@RestController
public class ClientShopUserController {

    @Autowired
    private SysLoginService loginService;
    @Autowired
    private AliConfigService aliConfigService;
    @Autowired
    private ShopUserService shopUserService;
    @Autowired
    private IShopUserService iShopUserService;

    /**
     * 获取验证码
     */
    @PostMapping("/getCode")
    public AjaxResult getCode(@RequestBody ShopUser shopUser){
        String result = aliConfigService.sendMsg(shopUser);
        if (result != null){
            return AjaxResult.success();
        }
        return AjaxResult.error("发送验证码失败");
    }

    /**
     * 登录方法
     *
     * @return 结果
     */
    @PostMapping("/shopLogin")
    public AjaxResult shopLogin(@RequestBody ShopUserResq userResq)
    {
        AjaxResult ajax = AjaxResult.success();
        ShopUser shopUser = shopUserService.login(userResq);
        if (shopUser == null || shopUser.getMsg() != null){
            return new AjaxResult(999,"登录失败");
        }else {
            // 生成令牌
            String token = loginService.shopUserLogin(shopUser.getUsername(), shopUser.getPassword());
            ajax.put("data", shopUser);
            ajax.put(Constants.TOKEN, token);
            return ajax;
        }
    }

    /**
     * 修改个人信息
     * @param shopUser 个人信息
     * @return 成功
     */
    @PostMapping("/updateUser")
    public AjaxResult modifyUserInfo(@RequestBody ShopUser shopUser){
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            return AjaxResult.error(401,"用户未登录");
        }
        shopUser.setUserId(userId);
        return AjaxResult.success(shopUserService.modifyUser(shopUser));
    }

    /**
     * 获取个人信息
     * @return 个人信息
     */
    @GetMapping("/getUserInfo")
    public AjaxResult getUserInfo() {
        // 获取当前登录用户名
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            return AjaxResult.error(401,"用户未登录");
        }
        
        // 根据用户ID查询完整的用户信息
        ShopUser shopUser = iShopUserService.selectShopUserByUserId(userId);
        if (shopUser == null) {
            return AjaxResult.error("未找到用户信息");
        }
        
        // 出于安全考虑，清除敏感信息
        shopUser.setPassword(null);
        
        return AjaxResult.success(shopUser);
    }
    
    /**
     * 根据用户ID获取个人信息
     * @param userId 用户ID
     * @return 个人信息
     */
    @GetMapping("/getUserInfoById/{userId}")
    public AjaxResult getUserInfoById(@PathVariable("userId") Long userId) {
        if (userId == null) {
            return AjaxResult.error("用户ID不能为空");
        }
        
        // 根据用户ID查询完整的用户信息
        ShopUser shopUser = iShopUserService.selectShopUserByUserId(userId);
        if (shopUser == null) {
            return AjaxResult.error("未找到用户信息");
        }
        
        // 出于安全考虑，清除敏感信息
        shopUser.setPassword(null);
        
        return AjaxResult.success(shopUser);
    }


    @PostMapping("/user/file")
    public AjaxResult addConfigFile(
            @RequestParam("file") MultipartFile file
    ) throws IOException {
        // 检查是否已经登录
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            return AjaxResult.error(401,"用户未登录");
        }
        // 1. 处理文件上传
        if (file != null && !file.isEmpty()) {
            // 保存文件逻辑
            String url = AliConfig.ossUp("user/", file.getOriginalFilename(), file.getInputStream());
            Map<String,Object> head = new HashMap<>();
            head.put("headImg",url);
            return AjaxResult.success("请求成功", head);
        }
        return AjaxResult.error("文件不能为空");
    }

}