import Foundation

enum NetworkError: LocalizedError {
    case invalidURL
    case invalidResponse
    case authenticationFailed
    case serverError(Int)
    case decodingError(Error)
    case encodingError
    case noInternetConnection
    case timeout
    case unknown(Error)
    case invalidCredentials
    case playlistParsingFailed(String)
    case tmdbAPIKeyMissing
    case tmdbRequestFailed(String)
    case rateLimited

    var errorDescription: String? {
        switch self {
        case .invalidURL:
            return "The URL is invalid. Please check the server configuration."
        case .invalidResponse:
            return "The server response was invalid. Please try again."
        case .authenticationFailed:
            return "Authentication failed. Please check your password and try again."
        case .serverError(let code):
            return "Server error occurred (Code: \(code)). Please try again later."
        case .decodingError(let error):
            return "Failed to decode server response: \(error.localizedDescription)"
        case .encodingError:
            return "Failed to encode request data."
        case .noInternetConnection:
            return "No internet connection. Please check your network settings."
        case .timeout:
            return "The request timed out. Please check your connection and try again."
        case .unknown(let error):
            return "An unknown error occurred: \(error.localizedDescription)"
        case .invalidCredentials:
            return "The stored credentials are invalid. Please log in again."
        case .playlistParsingFailed(let details):
            return "Failed to parse playlist: \(details)"
        case .tmdbAPIKeyMissing:
            return "TMDB API key is not configured."
        case .tmdbRequestFailed(let details):
            return "TMDB request failed: \(details)"
        case .rateLimited:
            return "Too many requests. Please wait a moment and try again."
        }
    }

    var recoverySuggestion: String? {
        switch self {
        case .noInternetConnection:
            return "Check your Wi-Fi or network connection and try again."
        case .authenticationFailed:
            return "Verify your password is correct and try again."
        case .serverError:
            return "The server is experiencing issues. Please try again later."
        case .timeout:
            return "Your connection may be slow. Try again or check your network."
        case .invalidCredentials:
            return "Please log out and log in again with your password."
        default:
            return "Please try again or contact support if the problem persists."
        }
    }
}
