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

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

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

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