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

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

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

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