package io.controller;

import cn.hutool.crypto.digest.DigestUtil;
import io.annotation.Login;
import io.annotation.LoginUser;
import io.common.utils.Result;
import io.common.validator.ValidatorUtils;
import io.modules.item.dto.LoginDTO;
import io.dto.RegisterDTO;
import io.modules.item.entity.UserEntity;
import io.service.TokenService;
import io.modules.item.service.FrontUserService;
import io.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Map;

/**
 * 用户管理
 */
@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理")
@AllArgsConstructor
@CrossOrigin
public class UserController {
    private final UserService userService;
    private final TokenService tokenService;

    @PostMapping("register")
    @Operation(summary = "注册")
    public Result register(@RequestBody RegisterDTO dto) {

        if (dto.getUsername().equals("admin")){
            return new Result().error("admin用户禁止创建~");
        }

        //表单校验
        ValidatorUtils.validateEntity(dto);
        if (!dto.getPassword().equals(dto.getConfirmPassword())) {
            return new Result().error("两次密码输入不一致~");
        }

        if (userService.getByUsername(dto.getUsername()) != null) {
            return new Result().error("用户名已经存在~");
        }
        //表单校验
        ValidatorUtils.validateEntity(dto);
        UserEntity user = new UserEntity();
        user.setUsername(dto.getUsername());
        user.setNickName(dto.getUsername());
        user.setPassword(DigestUtil.sha256Hex(dto.getPassword()));
        user.setCreateDate(new Date());
        userService.insert(user);
        return new Result();
    }


    @PostMapping("login")
    @Operation(summary = "登录")
    public Result<Map<String, Object>> login(@RequestBody LoginDTO dto) {
        //表单校验
        ValidatorUtils.validateEntity(dto);
        //用户登录
        Map<String, Object> map = userService.login(dto);
        return new Result().ok(map);
    }

    @Login
    @PutMapping("update")
    @Operation(summary = "修改信息")
    public Result update(@RequestBody UserEntity dto,@Parameter(hidden = true) @RequestAttribute("userId") Long userId) {
        //表单校验
        ValidatorUtils.validateEntity(dto);
        UserEntity user = new UserEntity();
        user.setId(userId);
        user.setUsername(dto.getUsername());
        user.setNickName(dto.getNickName());
        user.setIntroduce(dto.getIntroduce());
        if (StringUtils.isNotEmpty(dto.getPassword())){
            user.setPassword(DigestUtil.sha256Hex(dto.getPassword()));
        }
        userService.updateById(user);
        return new Result();
    }


    @Login
    @PostMapping("logout")
    @Operation(summary = "退出")
    public Result logout(@Parameter(hidden = true) @RequestAttribute("userId") Long userId) {
        tokenService.expireToken(userId);
        return new Result();
    }

    @Login
    @GetMapping("userInfo")
    @Operation(summary = "获取用户信息")
    public Result<UserEntity> userInfo(@Parameter(hidden = true) @LoginUser UserEntity user) {
        return new Result<UserEntity>().ok(user);
    }

    @Login
    @GetMapping("userId")
    @Operation(summary = "获取用户ID")
    public Result<Long> userInfo(@Parameter(hidden = true) @RequestAttribute("userId") Long userId) {
        return new Result<Long>().ok(userId);
    }
}
