import Foundation

func withRetry<T>(
    maxAttempts: Int, delay: Duration,
    operation: String,
    body: () async throws -> T,
) async throws -> T {
    var lastError: any Error = SeedError.retryExhausted(operation: operation, attempts: maxAttempts)
    for attempt in 1 ... maxAttempts {
        do {
            return try await body()
        } catch {
            lastError = error
            if attempt == maxAttempts { break }
            print("  \(operation) attempt \(attempt) failed, retrying...")
            try await Task.sleep(for: delay)
        }
    }
    throw lastError
}
