package com.yaoai.api.controller;

import com.yaoai.common.response.ApiResponse;
import com.yaoai.security.dto.LoginRequest;
import com.yaoai.security.dto.LoginResponse;
import com.yaoai.security.dto.RegisterRequest;
import com.yaoai.security.dto.UserInfo;
import com.yaoai.security.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "认证", description = "注册 / 登录 / 登出 / 当前用户")
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    @Operation(summary = "注册账号（同时创建租户）")
    @PostMapping("/register")
    public ApiResponse<LoginResponse> register(@Valid @RequestBody RegisterRequest req) {
        return ApiResponse.success(authService.register(req));
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public ApiResponse<LoginResponse> login(@Valid @RequestBody LoginRequest req) {
        return ApiResponse.success(authService.login(req));
    }

    @Operation(summary = "登出（需登录）")
    @PostMapping("/logout")
    public ApiResponse<Void> logout() {
        authService.logout();
        return ApiResponse.success();
    }

    @Operation(summary = "获取当前登录用户信息（需登录）")
    @GetMapping("/me")
    public ApiResponse<UserInfo> me() {
        return ApiResponse.success(authService.me());
    }
}
