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

export async function fetchBackupJobs(
	serviceId?: string,
): Promise<BackupJob[]> {
	return get<BackupJob[]>(
		"/api/backups/jobs",
		serviceId ? { service_id: serviceId } : undefined,
	);
}

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,
	serviceId?: string,
): Promise<BackupRun[]> {
	return get<BackupRun[]>("/api/backups/runs", {
		...(jobId ? { job_id: jobId } : {}),
		...(status ? { status } : {}),
		...(serviceId ? { service_id: serviceId } : {}),
	});
}

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,
	serviceId?: string,
): Promise<BackupAlert[]> {
	return get<BackupAlert[]>("/api/backups/alerts", {
		...(jobId ? { job_id: jobId } : {}),
		...(acknowledged !== undefined
			? { acknowledged: String(acknowledged) }
			: {}),
		...(severity ? { severity } : {}),
		...(serviceId ? { service_id: serviceId } : {}),
	});
}

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");
}
