package com.example.catchTheLetters.controller;

import com.example.catchTheLetters.entity.LoginDto;
import com.example.catchTheLetters.entity.User;
import com.example.catchTheLetters.service.AuthService;
import com.example.catchTheLetters.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 认证控制器
 * @author spyn
 */
@RestController
@Slf4j
@Api(tags = "认证API")
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private AuthService authService;

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public R<String> login(@RequestBody LoginDto loginDto) {
        return R.ok(authService.login(loginDto));
    }

    @ApiOperation(value = "用户注销")
    @PostMapping("/logout")
    public R logout(String token) {
        return authService.logout(token) ? R.ok() : R.fail();
    }

    @ApiOperation(value = "验证token")
    @PostMapping("/verify")
    public R<User> verify(String token) {
        return R.ok(authService.verify(token));
    }
}