export interface BackupHealthSnapshot {
  enabled: boolean;
  hasRun: boolean;
  consecutiveFailures: number;
  lastSuccessAt?: number;
  lastFailureAt?: number;
  lastError?: string;
}

let snapshot: BackupHealthSnapshot = {
  enabled: false,
  hasRun: false,
  consecutiveFailures: 0,
};

export function initBackupHealth(enabled: boolean): void {
  snapshot = {
    enabled,
    hasRun: false,
    consecutiveFailures: 0,
  };
}

export function markBackupSuccess(now = Date.now()): void {
  snapshot = {
    ...snapshot,
    hasRun: true,
    consecutiveFailures: 0,
    lastSuccessAt: now,
    lastError: undefined,
  };
}

export function markBackupFailure(error: string, now = Date.now()): void {
  snapshot = {
    ...snapshot,
    hasRun: true,
    consecutiveFailures: snapshot.consecutiveFailures + 1,
    lastFailureAt: now,
    lastError: error,
  };
}

export function getBackupHealthSnapshot(): BackupHealthSnapshot {
  return { ...snapshot };
}
