import Foundation

enum CryptoError: Error, LocalizedError {
    case invalidBase64
    case invalidHex
    case invalidKeyData(String)
    case invalidSignature
    case signatureVerificationFailed
    case encryptionFailed(String)
    case decryptionFailed(String)
    case invalidECP1Format
    case pbkdf2Failed
    case rsaKeyGenerationFailed
    case rsaOperationFailed(String)
    case x3dhFailed(String)
    case ratchetError(String)
    case senderKeyError(String)
    case maxSkipExceeded
    case duplicateMessage
    case invalidHeader(String)
    case stateImportFailed(String)
    case keyConversionFailed(String)

    var errorDescription: String? {
        switch self {
        case .invalidBase64: return "Invalid base64 encoding"
        case .invalidHex: return "Invalid hex encoding"
        case .invalidKeyData(let msg): return "Invalid key data: \(msg)"
        case .invalidSignature: return "Invalid signature format"
        case .signatureVerificationFailed: return "Signature verification failed"
        case .encryptionFailed(let msg): return "Encryption failed: \(msg)"
        case .decryptionFailed(let msg): return "Decryption failed: \(msg)"
        case .invalidECP1Format: return "Invalid ECP1 key format"
        case .pbkdf2Failed: return "PBKDF2 key derivation failed"
        case .rsaKeyGenerationFailed: return "RSA key generation failed"
        case .rsaOperationFailed(let msg): return "RSA operation failed: \(msg)"
        case .x3dhFailed(let msg): return "X3DH failed: \(msg)"
        case .ratchetError(let msg): return "Ratchet error: \(msg)"
        case .senderKeyError(let msg): return "Sender key error: \(msg)"
        case .maxSkipExceeded: return "Maximum message skip exceeded"
        case .duplicateMessage: return "Duplicate message detected"
        case .invalidHeader(let msg): return "Invalid header: \(msg)"
        case .stateImportFailed(let msg): return "State import failed: \(msg)"
        case .keyConversionFailed(let msg): return "Key conversion failed: \(msg)"
        }
    }
}

enum NetworkError: Error, LocalizedError {
    case notConnected
    case connectionFailed(String)
    case timeout
    case serverError(String)
    case protocolError(String)
    case messageTooLarge
    case invalidResponse(String)
    case authenticationFailed(String)
    case alreadyConnected

    var errorDescription: String? {
        switch self {
        case .notConnected: return "Not connected to server"
        case .connectionFailed(let msg): return "Connection failed: \(msg)"
        case .timeout: return "Request timed out"
        case .serverError(let msg): return "Server error: \(msg)"
        case .protocolError(let msg): return "Protocol error: \(msg)"
        case .messageTooLarge: return "Message exceeds maximum size"
        case .invalidResponse(let msg): return "Invalid response: \(msg)"
        case .authenticationFailed(let msg): return "Authentication failed: \(msg)"
        case .alreadyConnected: return "Already connected"
        }
    }
}

enum ChatError: Error, LocalizedError {
    case notLoggedIn
    case conversationNotFound
    case membershipRequired
    case permissionDenied(String)
    case operationFailed(String)
    case fileError(String)
    case invalidData(String)

    var errorDescription: String? {
        switch self {
        case .notLoggedIn: return "Not logged in"
        case .conversationNotFound: return "Conversation not found"
        case .membershipRequired: return "Must be a member of this conversation"
        case .permissionDenied(let msg): return "Permission denied: \(msg)"
        case .operationFailed(let msg): return "Operation failed: \(msg)"
        case .fileError(let msg): return "File error: \(msg)"
        case .invalidData(let msg): return "Invalid data: \(msg)"
        }
    }
}
