import 'package:freezed_annotation/freezed_annotation.dart';

part 'socket_error.freezed.dart';

/// WebSocket 统一错误类型（Freezed 联合类型）
@freezed
class SocketError with _$SocketError implements Exception {
  const factory SocketError.connectionFailed(String message) =
  _ConnectionFailed;
  const factory SocketError.connectionTimeout() = _ConnectionTimeout;
  const factory SocketError.disconnected({String? reason}) = _Disconnected;
  const factory SocketError.pingTimeout() = _PingTimeout;
  const factory SocketError.networkUnavailable() = _NetworkUnavailable;
  const factory SocketError.invalidURL(String url) = _InvalidURL;
  const factory SocketError.sendFailed(String message) = _SendFailed;
  const factory SocketError.unknown(String? message) = _Unknown;
}

/// 错误类型扩展 — 提供用户可读的错误描述
extension SocketErrorExtension on SocketError {
  String get displayMessage {
    return when(
      connectionFailed: (message) => 'Connection failed: $message',
      connectionTimeout: () => 'Connection timeout',
      disconnected: (reason) => reason ?? 'Disconnected',
      pingTimeout: () => 'Ping timeout',
      networkUnavailable: () => 'Network unavailable',
      invalidURL: (url) => 'Invalid URL: $url',
      sendFailed: (message) => 'Send failed: $message',
      unknown: (message) => message ?? 'Unknown error',
    );
  }
}