import { auth } from "@/lib/firebase"; const BASE = process.env.NEXT_PUBLIC_C2_URL ?? "http://localhost:8000"; async function request(path: string, options?: RequestInit): Promise { const user = auth.currentUser; const token = user ? await user.getIdToken() : null; const res = await fetch(`${BASE}${path}`, { ...options, headers: { "Content-Type": "application/json", ...(token ? { Authorization: `Bearer ${token}` } : {}), ...(options?.headers as Record | undefined), }, }); if (!res.ok) throw new Error(`C2 API error ${res.status}: ${await res.text()}`); if (res.status === 204) return undefined as T; return res.json(); } export const c2api = { // Nodes getNodes: () => request("/nodes"), getNode: (id: string) => request(`/nodes/${id}`), sendCommand: (nodeId: string, payload: object) => request(`/nodes/${nodeId}/command`, { method: "POST", body: JSON.stringify(payload) }), assignSystem: (nodeId: string, systemId: string) => request(`/nodes/${nodeId}/config/${systemId}`, { method: "POST" }), // Systems getSystems: () => request("/systems"), createSystem: (body: object) => request("/systems", { method: "POST", body: JSON.stringify(body) }), updateSystem: (id: string, body: object) => request(`/systems/${id}`, { method: "PUT", body: JSON.stringify(body) }), deleteSystem: (id: string) => request(`/systems/${id}`, { method: "DELETE" }), // Tokens getTokens: () => request("/tokens"), addToken: (body: { name: string; token: string }) => request("/tokens", { method: "POST", body: JSON.stringify(body) }), deleteToken: (id: string) => request(`/tokens/${id}`, { method: "DELETE" }), // Node approval approveNode: (id: string) => request(`/nodes/${id}/approve`, { method: "POST" }), rejectNode: (id: string) => request(`/nodes/${id}/reject`, { method: "POST" }), // Calls getCalls: (params?: Record) => { const qs = params ? "?" + new URLSearchParams(params).toString() : ""; return request(`/calls${qs}`); }, };