package cn.novalon.gym.manage.auth.handler;

import cn.novalon.gym.manage.auth.dto.LoginRequest;
import cn.novalon.gym.manage.auth.dto.LoginResponse;
import cn.novalon.gym.manage.auth.dto.UserInfo;
import cn.novalon.gym.manage.auth.service.IAuthService;
import cn.novalon.gym.manage.auth.util.ResponseCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Validator;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

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

@Component
@Tag(name = "认证管理", description = "用户认证相关操作")
public class AuthHandler {

    private final IAuthService authService;
    private final Validator validator;

    public AuthHandler(IAuthService authService, Validator validator) {
        this.authService = authService;
        this.validator = validator;
    }

    @Operation(summary = "用户名+密码登录", description = "使用用户名和密码进行登录认证")
    public Mono<ServerResponse> login(ServerRequest request) {
        return request.bodyToMono(LoginRequest.class)
                .flatMap(loginRequest -> {
                    return authService.login(loginRequest)
                            .flatMap(response -> {
                                Map<String, Object> result = new HashMap<>();
                                result.put("code", ResponseCode.SUCCESS);
                                result.put("message", "登录成功");
                                result.put("data", response);
                                return ServerResponse.ok().bodyValue(result);
                            })
                            .onErrorResume(error -> {
                                Map<String, Object> errorResponse = new HashMap<>();
                                errorResponse.put("code", ResponseCode.BAD_REQUEST);
                                errorResponse.put("message", error.getMessage());
                                return ServerResponse.badRequest().bodyValue(errorResponse);
                            });
                })
                .onErrorResume(error -> {
                    Map<String, Object> errorResponse = new HashMap<>();
                    errorResponse.put("code", ResponseCode.BAD_REQUEST);
                    errorResponse.put("message", "请求参数错误: " + error.getMessage());
                    return ServerResponse.badRequest().bodyValue(errorResponse);
                });
    }

    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户详细信息")
    public Mono<ServerResponse> getUserInfo(ServerRequest request) {
        try {
            Long userId = Long.valueOf(request.pathVariable("id"));
            return authService.getUserInfo(userId)
                    .flatMap(userInfo -> {
                        Map<String, Object> result = new HashMap<>();
                        result.put("code", ResponseCode.SUCCESS);
                        result.put("message", "获取用户信息成功");
                        result.put("data", userInfo);
                        return ServerResponse.ok().bodyValue(result);
                    })
                    .onErrorResume(error -> {
                        Map<String, Object> errorResponse = new HashMap<>();
                        errorResponse.put("code", ResponseCode.BAD_REQUEST);
                        errorResponse.put("message", error.getMessage());
                        return ServerResponse.badRequest().bodyValue(errorResponse);
                    });
        } catch (NumberFormatException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", ResponseCode.BAD_REQUEST);
            errorResponse.put("message", "无效的用户ID");
            return ServerResponse.badRequest().bodyValue(errorResponse);
        }
    }
}