import { apiRequest } from '../../lib/api'; import { filterAppsByCurrentAdmin } from '../../lib/admin-app-permissions'; import type { ApiApp, RuntimeEnvProfile, RuntimeEvent, RuntimePodMetric, RuntimeResourceResponse, RuntimeWorkloadLogResponse } from '../../types/runtime'; export function loadApps() { return apiRequest('/apps').then(filterAppsByCurrentAdmin); } export function loadRuntimeProfiles(appId?: string) { const query = appId ? `?appId=${encodeURIComponent(appId)}` : ''; return apiRequest(`/apps/runtime-profiles${query}`); } export function createRuntimeProfile(input: { appId: string; name: string; environment: string; namespace: string }) { return apiRequest<{ profile: RuntimeEnvProfile }>('/apps/runtime-profiles', { method: 'POST', body: JSON.stringify(input) }); } export function deleteRuntimeProfile(profileId: string) { return apiRequest<{ deleted: boolean; profileId: string; name: string }>(`/apps/runtime-profiles/${profileId}`, { method: 'DELETE' }); } export function createRuntimeVariable( profileId: string, input: { componentId: string; key: string; value: string; isSecret: boolean } ) { return apiRequest(`/apps/runtime-profiles/${profileId}/variables`, { method: 'POST', body: JSON.stringify(input) }); } export function createRuntimeIngressRule( profileId: string, input: { componentId: string; host: string; path?: string; servicePort: number; stripPrefix?: boolean; tlsEnabled?: boolean; tlsSecretName?: string } ) { return apiRequest(`/apps/runtime-profiles/${profileId}/ingress-rules`, { method: 'POST', body: JSON.stringify(input) }); } export function deleteRuntimeIngressRule(profileId: string, ruleId: string) { return apiRequest(`/apps/runtime-profiles/${profileId}/ingress-rules/${ruleId}`, { method: 'DELETE' }); } export function deleteRuntimeVariable(profileId: string, variableId: string) { return apiRequest(`/apps/runtime-profiles/${profileId}/variables/${variableId}`, { method: 'DELETE' }); } export function applyRuntimeProfile(profileId: string) { return apiRequest<{ profile: RuntimeEnvProfile; logs: string }>(`/apps/runtime-profiles/${profileId}/apply`, { method: 'PATCH' }); } export function loadRuntimeResources(namespace: string) { return apiRequest(`/apps/runtime-namespaces/${encodeURIComponent(namespace)}/resources`); } export function loadRuntimeResourceYaml(namespace: string, kind: string, name: string) { return apiRequest<{ namespace: string; kind: string; name: string; yaml: string }>( `/apps/runtime-namespaces/${encodeURIComponent(namespace)}/resources/${encodeURIComponent(kind)}/${encodeURIComponent(name)}/yaml` ); } export function loadRuntimePodMetrics(namespace?: string) { const query = namespace ? `?namespace=${encodeURIComponent(namespace)}` : ''; return apiRequest(`/runtime/metrics/pods${query}`); } export function loadRuntimeEvents(namespace?: string) { const query = namespace ? `?namespace=${encodeURIComponent(namespace)}` : ''; return apiRequest(`/runtime/events${query}`); } export function loadRuntimeWorkloadLogs(namespace: string, kind: string, name: string, tail = 100) { return apiRequest( `/runtime/workloads/${encodeURIComponent(namespace)}/${encodeURIComponent(kind)}/${encodeURIComponent(name)}/logs?tail=${tail}` ); } export function ensureRuntimeNamespace(namespace: string) { return apiRequest<{ namespace: string; created: boolean; logs: string }>('/apps/runtime-namespaces', { method: 'POST', body: JSON.stringify({ namespace }) }); } export function scaleRuntimeWorkload(namespace: string, kind: string, name: string, replicas: number) { return apiRequest(`/runtime/workloads/${encodeURIComponent(namespace)}/${encodeURIComponent(kind)}/${encodeURIComponent(name)}/scale`, { method: 'POST', body: JSON.stringify({ replicas }) }); } export function restartRuntimeWorkload(namespace: string, kind: string, name: string) { return apiRequest(`/runtime/workloads/${encodeURIComponent(namespace)}/${encodeURIComponent(kind)}/${encodeURIComponent(name)}/restart`, { method: 'POST' }); }