import { get, post } from "./shared";
import type {
	BackupAlert,
	BackupDashboardSummary,
	BackupJob,
	BackupRun,
} from "../types/backups";

export async function fetchBackupJobs(): Promise<BackupJob[]> {
	return get<BackupJob[]>("/api/backups/jobs");
}

export async function fetchBackupJob(
	jobId: string,
): Promise<{ job: BackupJob; runs: BackupRun[] }> {
	return get<{ job: BackupJob; runs: BackupRun[] }>(
		`/api/backups/jobs/${jobId}`,
	);
}

export async function fetchBackupRuns(
	jobId?: string,
	status?: string,
): Promise<BackupRun[]> {
	return get<BackupRun[]>("/api/backups/runs", {
		...(jobId ? { job_id: jobId } : {}),
		...(status ? { status } : {}),
	});
}

export async function fetchBackupRun(runId: string): Promise<BackupRun> {
	return get<BackupRun>(`/api/backups/runs/${runId}`);
}

export async function fetchBackupAlerts(
	jobId?: string,
	acknowledged?: boolean,
	severity?: string,
): Promise<BackupAlert[]> {
	return get<BackupAlert[]>("/api/backups/alerts", {
		...(jobId ? { job_id: jobId } : {}),
		...(acknowledged !== undefined
			? { acknowledged: String(acknowledged) }
			: {}),
		...(severity ? { severity } : {}),
	});
}

export async function acknowledgeBackupAlert(
	alertId: string,
): Promise<BackupAlert> {
	return post<BackupAlert>(`/api/backups/alerts/${alertId}/acknowledge`);
}

export async function fetchBackupDashboard(): Promise<BackupDashboardSummary> {
	return get<BackupDashboardSummary>("/api/dashboard/backups");
}
