package io.modules.security.controller;

import io.common.exception.RenException;
import io.common.utils.IpUtils;
import io.common.utils.Result;
import io.common.validator.AssertUtils;
import io.common.validator.ValidatorUtils;
import io.modules.security.dto.LoginDTO;
import io.modules.security.password.PasswordUtils;
import io.modules.security.service.CaptchaService;
import io.modules.security.service.SysUserTokenService;
import io.modules.security.user.SecurityUser;
import io.modules.security.user.UserDetail;
import io.modules.sys.dto.SysUserDTO;
import io.modules.sys.enums.UserStatusEnum;
import io.modules.sys.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.Date;

/**
 * 登录
 */
@RestController
@Tag(name = "登录管理")
@AllArgsConstructor
public class LoginController {
    private final SysUserService sysUserService;
    private final SysUserTokenService sysUserTokenService;
    private final CaptchaService captchaService;

    @GetMapping("captcha")
    @Operation(summary = "验证码")
    @Parameter(in = ParameterIn.QUERY, ref = "string", name = "uuid", required = true)
    public void captcha(HttpServletResponse response, String uuid) throws IOException {
        //uuid不能为空
        AssertUtils.isBlank(uuid, "UUID不能为空~");
        //生成验证码
        captchaService.create(response, uuid);
    }
    @PostMapping("login")
    @Operation(summary = "登录")
    public Result login(@RequestBody LoginDTO login) {
        //效验数据
//        ValidatorUtils.validateEntity(login);
        //验证码是否正确
    //        boolean flag = captchaService.validate(login.getUuid(), login.getCaptcha());
    //        if (!flag) {
    //            return new Result().error("验证码不正确~");
    //        }
        //用户信息
        SysUserDTO user = sysUserService.getByUsername(login.getUsername());

        //用户不存在
        if (user == null) {
            throw new RenException("用户不存在~");
        }
        //密码错误
        if (!PasswordUtils.matches(login.getPassword(), user.getPassword())) {
            throw new RenException("密码错误~");
        }
        //账号停用
        if (user.getStatus() == UserStatusEnum.DISABLE.value()) {
            throw new RenException("账号停用~");
        }
        //登录成功
        return sysUserTokenService.createToken(user.getId());
    }

    @PostMapping("logout")
    @Operation(summary = "退出")
    public Result logout(HttpServletRequest request) {
        UserDetail user = SecurityUser.getUser();
        //退出
        sysUserTokenService.logout(user.getId());
        return new Result();
    }
}
