import Foundation

enum SyncProviderType: String, CaseIterable, Identifiable {
    case none
    case server
    case iCloud

    var id: String { rawValue }
    var displayName: String {
        switch self {
        case .none: return "None"
        case .server: return "Self-Hosted Server"
        case .iCloud: return "iCloud (BETA)"
        }
    }
}

protocol SyncProvider {
    var type: SyncProviderType { get }
    var isConfigured: Bool { get }
    var isConnected: Bool { get }
    var lastSyncTime: Date? { get }

    func sync(dataManager: ClimbingDataManager) async throws
    func testConnection() async throws
    func disconnect()
}

enum SyncError: LocalizedError {
    case notConfigured
    case notConnected
    case invalidURL
    case invalidResponse
    case unauthorized
    case badRequest
    case serverError(Int)
    case decodingError(Error)
    case exportFailed
    case importFailed(Error)
    case imageNotFound
    case imageUploadFailed
    case providerError(String)

    var errorDescription: String? {
        switch self {
        case .notConfigured:
            return "Sync server not configured. Please set server URL and auth token."
        case .notConnected:
            return "Not connected to sync server. Please test connection first."
        case .invalidURL:
            return "Invalid server URL."
        case .invalidResponse:
            return "Invalid response from server."
        case .unauthorized:
            return "Authentication failed. Check your auth token."
        case .badRequest:
            return "Bad request. Check your data format."
        case .serverError(let code):
            return "Server error (code \(code))."
        case .decodingError(let error):
            return "Failed to decode response: \(error.localizedDescription)"
        case .exportFailed:
            return "Failed to export local data."
        case .importFailed(let error):
            return "Failed to import data: \(error.localizedDescription)"
        case .imageNotFound:
            return "Image not found on server."
        case .imageUploadFailed:
            return "Failed to upload image to server."
        case .providerError(let message):
            return "Sync provider error: \(message)"
        }
    }
}
