import { apiClient } from "./client";

export interface Balance {
  tenantId: number;
  balance: string;
  totalRecharged: string;
  totalSpent: string;
}

export interface BillingRecord {
  id: number;
  projectId: number | null;
  projectName?: string | null;
  operation: string;
  modality?: string | null;
  modelProvider?: string | null;
  modelId?: string | null;
  modelName?: string | null;
  unitCount?: number | null;
  success?: boolean | null;
  billingMeta?: string | null;
  credits: string;
  billingMode: string;
  refId: string | null;
  createdAt: string;
}

export interface RecordsPage {
  total: number;
  items: BillingRecord[];
}

export const usageApi = {
  getBalance: async (): Promise<Balance> => {
    const r = await apiClient.get("/usage/balance");
    return r.data.data;
  },
  getRecords: async (limit = 20, offset = 0): Promise<RecordsPage> => {
    const r = await apiClient.get("/usage/records", { params: { limit, offset } });
    return r.data.data;
  },
  purchase: async (packageId: string): Promise<Balance> => {
    const r = await apiClient.post("/usage/purchase", { packageId });
    return r.data.data;
  },
  getCosts: async (): Promise<Record<string, string>> => {
    const r = await apiClient.get("/usage/costs");
    return r.data.data ?? {};
  },
};
