import { requestJson } from "@/shared/api/http-client" import type { BackupScheduleSettingsDto, BackupStorageSettingsDto, PutBackupStorageSettings, } from "@mmapp/contracts/backups" export type BackupItem = { id: string serverId: number | string | null serverName: string filename: string sizeBytes: number createdAt: string kind: "manual" | "auto" notes?: string storage?: "local" | "s3" | "both" s3Key?: string | null uploadError?: string | null } type CreateBackupResponse = { created: BackupItem[] failures: Array<{ serverId: string; error: string }> } export type CreateBackupJobResponse = { jobId: string status: "queued" | "running" | "done" | "failed" total: number completed: number } export type BackupJobStatusResponse = { id: string status: "queued" | "running" | "done" | "failed" requestedAt: string startedAt?: string finishedAt?: string total: number completed: number created: BackupItem[] failures: Array<{ serverId: string; error: string }> } export async function listBackups(baseUrl: string): Promise { return requestJson(baseUrl, "/api/backups") } export async function createBackups( baseUrl: string, payload: { serverIds: string[]; notes?: string }, ): Promise { return requestJson(baseUrl, "/api/backups/create", { method: "POST", body: JSON.stringify(payload), }) } export async function createBackupsAsync( baseUrl: string, payload: { serverIds: string[]; notes?: string }, ): Promise { return requestJson(baseUrl, "/api/backups/create", { method: "POST", body: JSON.stringify(payload), }) } export async function getBackupJob(baseUrl: string, jobId: string): Promise { return requestJson(baseUrl, `/api/backups/jobs/${jobId}`) } export async function deleteBackup(baseUrl: string, id: string): Promise { await requestJson(baseUrl, `/api/backups/${id}`, { method: "DELETE" }) } export async function restoreBackup(baseUrl: string, id: string): Promise<{ filename: string; serverName: string }> { return requestJson<{ filename: string; serverName: string }>(baseUrl, `/api/backups/${id}/restore`, { method: "POST", }) } export async function getBackupScheduleSettings(baseUrl: string): Promise { return requestJson(baseUrl, "/api/backups/schedule") } export async function putBackupScheduleSettings( baseUrl: string, payload: Partial, ): Promise { return requestJson(baseUrl, "/api/backups/schedule", { method: "PUT", body: JSON.stringify(payload), }) } export async function getBackupStorageSettings(baseUrl: string): Promise { return requestJson(baseUrl, "/api/backups/storage") } export async function putBackupStorageSettings( baseUrl: string, payload: PutBackupStorageSettings, ): Promise { return requestJson(baseUrl, "/api/backups/storage", { method: "PUT", body: JSON.stringify(payload), }) } export async function testBackupStorage(baseUrl: string): Promise { return requestJson(baseUrl, "/api/backups/storage/test", { method: "POST" }) } export async function syncBackupsFromStorage( baseUrl: string, ): Promise<{ imported: number; skipped: number }> { return requestJson<{ imported: number; skipped: number }>(baseUrl, "/api/backups/storage/sync", { method: "POST", }) }