package com.sckw.auth.controller;

import com.sckw.auth.model.vo.req.*;
import com.sckw.auth.service.IAuthService;
import com.sckw.core.model.enums.LoginMethodEnum;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @desc: 用户权限
 * @author: czh
 * @date: 2023/6/19
 */
@RestController
@RequestMapping("/auth")
@Tag(name = "登录相关接口")
public class AuthController {

    @Autowired
    private IAuthService authService;

    /**
     * @param reqVo 登录入参
     * @return HttpResult
     * @desc: 用户登录
     * @author: czh
     * @date: 2023/6/16
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public HttpResult login(@Valid @RequestBody LoginReqVo reqVo, HttpServletRequest request) {
        int systemType = request.getIntHeader("System-Type");
        String clientType = request.getHeader("Client-Type");

        reqVo.setSystemType(systemType);
        reqVo.setClientType(clientType);

        LoginBase loginBase = new LoginBase();
        loginBase.setAccount(reqVo.getAccount());
        loginBase.setPassword(reqVo.getPassword());
        loginBase.setCaptcha(reqVo.getCaptcha());
        loginBase.setSystemType(systemType);
        loginBase.setClientType(clientType);
        loginBase.setLoginMethod(LoginMethodEnum.ORDINARY.getValue());
        loginBase.setRememberMe(reqVo.getRememberMe());
        loginBase.setDeviceId(reqVo.getDeviceId());
        return authService.login(loginBase);
    }

    /**
     * @param reqVo 刷新Token入参
     * @return HttpResult
     * @desc: 刷新Token（记住密码时使用，支持多设备）
     * @date: 2026/1/14
     */
    @PostMapping("/refresh")
    @Operation(summary = "刷新Token")
    public HttpResult refreshToken(@Valid @RequestBody RefreshTokenReqVo reqVo, HttpServletRequest request) {
        int systemType = request.getIntHeader("System-Type");
        String clientType = request.getHeader("Client-Type");
        return authService.refreshToken(reqVo.getRefreshToken(), clientType, systemType, reqVo.getDeviceId());
    }

    /**
     * @param reqVo 切换账号入参
     * @return HttpResult
     * @desc: 切换账号（使用目标账号的refreshToken快速登录，无需重新输入密码）
     * @date: 2026/1/14
     */
    @Operation(summary = "切换账号")
    @PostMapping("/switchAccount")
    public HttpResult switchAccount(@Valid @RequestBody SwitchAccountReqVo reqVo, HttpServletRequest request) {
        int systemType = request.getIntHeader("System-Type");
        String clientType = request.getHeader("Client-Type");
        return authService.switchAccount(reqVo.getTargetRefreshToken(), clientType, systemType, reqVo.getDeviceId());
    }

    /**
     * 切换角色
     *
     * @param reqVo
     * @return
     */
    @PostMapping("/changeRole")
    public HttpResult changeRole(@Valid @RequestBody RoleChangeVo reqVo, HttpServletRequest request) {
        int systemType = request.getIntHeader("System-Type");
        String clientType = request.getHeader("Client-Type");

        reqVo.setSystemType(systemType);
        reqVo.setClientType(clientType);
        return authService.changeRole(reqVo);
    }

    /**
     * @return HttpResult
     * @desc: 根据token获取登录信息
     * @author: czh
     * @date: 2023/9/27
     */
    @PostMapping("getLoginResByToken")
    public HttpResult getLoginResByToken(HttpServletRequest request) {
        String clientType = request.getHeader("Client-Type");
        String token = request.getHeader("Access-Token");
        return HttpResult.ok(authService.getLoginResByToken(clientType, token));
    }

    /**
     * @param id 主键ID
     * @desc 重置密码
     * @author zk
     * @date 2023/06/02
     **/
    @PostMapping("/resetPassword")
    public HttpResult resetPassword(@RequestParam Long id) {
        authService.resetPassword(id);
        return HttpResult.ok(HttpStatus.MSG_001);
    }

}
