import { fetchJson } from "./client";
import type { ApiKeyRecord, ApiResult } from "@/types";

export const apiKeysAPI = {
  list: (): Promise<ApiResult<{ keys?: ApiKeyRecord[] }>> => fetchJson("/v1/api-keys", { timeoutMs: 12000, timeoutName: "api keys" }),
  create: (name: string): Promise<ApiResult<{ key?: ApiKeyRecord; secret?: string }>> => fetchJson("/v1/api-keys", { method: "POST", body: JSON.stringify({ name }), timeoutMs: 12000, timeoutName: "create api key" }),
  rename: (id: string, name: string): Promise<ApiResult<{ key?: ApiKeyRecord }>> => fetchJson(`/v1/api-keys/${encodeURIComponent(id)}`, { method: "PATCH", body: JSON.stringify({ name }), timeoutMs: 12000, timeoutName: "rename api key" }),
  revoke: (id: string): Promise<ApiResult<{ ok?: boolean }>> => fetchJson(`/v1/api-keys/${encodeURIComponent(id)}`, { method: "DELETE", body: JSON.stringify({}), timeoutMs: 12000, timeoutName: "revoke api key" })
};
