package com.CUST.brain.web.controller.system;

import com.CUST.brain.common.api.CommonResult;
import com.CUST.brain.common.utils.JwtTokenUtil;
import com.CUST.brain.common.dto.system.LoginRequestDTO;
import com.CUST.brain.common.dto.system.LoginResponseDTO;
import com.CUST.brain.common.dto.system.UserInfoResponseDTO;
import com.CUST.brain.common.utils.SecurityUtils;
import com.CUST.brain.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 系统认证控制器
 */
@Api(tags = "系统认证管理")
@RestController
@RequestMapping("/api/system/auth")
public class LoginController {

    @Resource
    private SysUserService userService;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public CommonResult<LoginResponseDTO> login(@Valid @RequestBody LoginRequestDTO loginDTO) {
        return CommonResult.success(userService.login(loginDTO));
    }

    @PostMapping("/logout")
    @ApiOperation("用户登出")
    public CommonResult<Void> logout(@RequestHeader("Authorization") String token) {
        String userId = jwtTokenUtil.getUserIdFromToken(token);
        userService.logout(token, userId);
        return CommonResult.success(null);
    }

    @GetMapping("/info")
    @ApiOperation("获取当前用户信息")
    public CommonResult<UserInfoResponseDTO> getUserInfo() {
        String userId = SecurityUtils.getCurrentUserId();
        UserInfoResponseDTO userInfo = userService.getUserInfo(userId);
        return CommonResult.success(userInfo);
    }
} 