/**
 * Metrics API
 *
 * 数据看板/上报相关接口
 *
 */
import { AppEvent, PwaEvent } from "@/data/schemas/metrics";
import { ApiPath } from "./api_path";
import { httpClient } from "./http_client";
import { ApiEnvelope, unwrapOptional } from "./response_helper";

export class MetricsApi {
  /**
   * 上报 PWA 事件
   */
  async reportPwaEvent(body: PwaEvent): Promise<void> {
    const env = await httpClient<ApiEnvelope<unknown>>(
      ApiPath.metricsPwaEvent,
      { method: "POST", body },
    );
    unwrapOptional(env);
  }

  /**
   * 上报用户信息
   */
  async reportUserInfo(body: AppEvent): Promise<void> {
    const env = await httpClient<ApiEnvelope<unknown>>(ApiPath.reportUserInfo, {
      method: "POST",
      body,
    });
    unwrapOptional(env);
  }
}

/**
 * 全局单例
 */
export const metricsApi = new MetricsApi();
