/**
 * User API
 *
 * 用户相关接口
 * 
 */
import { httpClient } from "./http_client";
import { ApiPath } from "./api_path";
import { ApiEnvelope, unwrap } from "./response_helper";
import {
  User,
  UserEntitlements,
} from "@/data/dto/user";
import type { UserData } from "@/data/schemas/user/user";

export class UserApi {
  /**
   * 获取个人信息
   */
  async getCurrentUser(): Promise<User> {
    const env = await httpClient<ApiEnvelope<unknown>>(ApiPath.userProfile);
    return User.fromJson(unwrap(env) as UserData);
  }

  /**
   * 获取当前用户权益快照
   */
  async getEntitlements(): Promise<UserEntitlements> {
    const env = await httpClient<ApiEnvelope<unknown>>(ApiPath.userEntitlements);
    return UserEntitlements.fromJson(unwrap(env) as Record<string, unknown>);
  }
}

/**
 * 全局单例
 */
export const userApi = new UserApi();
