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.dto.LoginDTO;
import io.dto.RegisterDTO;
import io.entity.UserEntity;
import io.service.TokenService;
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.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) {
        //表单校验
        ValidatorUtils.validateEntity(dto);
        UserEntity user = new UserEntity();
        user.setMobile(dto.getMobile());
        user.setNickName(dto.getNickName());
        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);
    }

    @PostMapping("updateInfo")
    @Operation(summary = "修改信息")
    public Result update(@RequestBody RegisterDTO dto) {
        //表单校验
        ValidatorUtils.validateEntity(dto);
        UserEntity user = new UserEntity();
        user.setId(dto.getId());
        user.setMobile(dto.getMobile());
        user.setNickName(dto.getNickName());
        user.setIntroduce(dto.getIntroduce());
        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);
    }

    @GetMapping("notToken")
    @Operation(summary = "忽略Token验证测试")
    public Result<String> notToken() {
        return new Result<String>().ok("无需token也能访问。。。");
    }

}
