package top.tqx.demo_1.controller;

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.web.bind.annotation.*;
import top.tqx.demo_1.annotation.RateLimit;
import top.tqx.demo_1.common.Result;
import top.tqx.demo_1.dto.LoginRequest;
import top.tqx.demo_1.service.AuthService;
import top.tqx.demo_1.vo.LoginResponse;
import top.tqx.demo_1.vo.UserInfoVO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "认证管理", description = "用户登录、登出、获取用户信息")
@RestController
@RequestMapping("/auth")
public class AuthController extends BaseController {

    public AuthController(AuthService authService) {
        this.authService = authService;
    }

    @Operation(summary = "用户登录", description = "使用用户名和密码登录系统")
    @PostMapping("/login")
    @RateLimit(maxRequests = 5, timeWindow = 60, key = "ip")
    public Result<LoginResponse> login(@Valid @RequestBody LoginRequest request) {
        LoginResponse response = authService.login(request.getUsername(), request.getPassword());
        return success(response);
    }

    @Operation(summary = "用户登出", description = "退出登录，清除Token")
    @PostMapping("/logout")
    public Result<Void> logout(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            authService.logout(token.substring(7));
        }
        return success();
    }

    @Operation(summary = "获取用户信息", description = "获取当前登录用户的详细信息")
    @GetMapping("/user/info")
    public Result<UserInfoVO> getUserInfo(HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        if (userId == null) {
            return error("用户未登录");
        }
        UserInfoVO userInfo = authService.getUserInfo(userId);
        return success(userInfo);
    }

    @Operation(summary = "获取用户角色", description = "获取当前用户的所有角色名称")
    @GetMapping("/user/roles")
    public Result<List<String>> getUserRoles(HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        if (userId == null) {
            return error("用户未登录");
        }
        List<String> roleNames = authService.getUserRoleNames(userId);
        return success(roleNames);
    }

    @Operation(summary = "获取主要角色", description = "获取当前用户的主要角色名称")
    @GetMapping("/user/main-role")
    public Result<Map<String, String>> getUserMainRole(HttpServletRequest request) {
        System.out.println("开始处理获取主要角色请求");
        Long userId = getCurrentUserId(request);
        System.out.println("获取到的用户ID: " + userId);
        if (userId == null) {
            System.out.println("用户未登录");
            return error("用户未登录");
        }
        String mainRoleName = authService.getUserMainRoleName(userId);
        System.out.println("获取到的主要角色名称: " + mainRoleName);
        Map<String, String> data = new HashMap<>();
        data.put("roleName", mainRoleName);
        return success(data);
    }
}
