import 'package:equatable/equatable.dart';

/// Server type enumeration
enum ServerType { geviscope, gcore, geviserver }

/// Configuration for a single recording server
class ServerConfig extends Equatable {
  final String id;
  final String name;
  final ServerType type;
  final bool enabled;
  final String address;
  final int port;
  final String username;
  final String bridgeUrl;
  final String? websocketUrl;
  final int cameraRangeStart;
  final int cameraRangeEnd;
  final int monitorRangeStart;
  final int monitorRangeEnd;

  const ServerConfig({
    required this.id,
    required this.name,
    required this.type,
    required this.enabled,
    required this.address,
    required this.port,
    required this.username,
    required this.bridgeUrl,
    this.websocketUrl,
    required this.cameraRangeStart,
    required this.cameraRangeEnd,
    required this.monitorRangeStart,
    required this.monitorRangeEnd,
  });

  /// Check if this server owns a camera ID
  bool ownsCamera(int cameraId) {
    return cameraId >= cameraRangeStart && cameraId <= cameraRangeEnd;
  }

  /// Check if this server owns a monitor ID
  bool ownsMonitor(int monitorId) {
    return monitorId >= monitorRangeStart && monitorId <= monitorRangeEnd;
  }

  factory ServerConfig.fromJson(Map<String, dynamic> json) {
    final typeStr = json['type'] as String;
    final type = ServerType.values.firstWhere(
      (t) => t.name == typeStr,
      orElse: () => ServerType.geviscope,
    );

    final connection = json['connection'] as Map<String, dynamic>? ?? {};
    final bridge = json['bridge'] as Map<String, dynamic>? ?? {};
    final resources = json['resources'] as Map<String, dynamic>? ?? {};
    final cameraRange = resources['cameraRange'] as Map<String, dynamic>? ?? {};
    final monitorRange = resources['monitorRange'] as Map<String, dynamic>? ?? {};

    return ServerConfig(
      id: json['id'] as String,
      name: json['name'] as String? ?? json['id'] as String,
      type: type,
      enabled: json['enabled'] as bool? ?? true,
      address: connection['address'] as String? ?? '',
      port: connection['port'] as int? ?? 7700,
      username: connection['username'] as String? ?? '',
      bridgeUrl: bridge['url'] as String? ?? '',
      websocketUrl: bridge['websocket'] as String?,
      cameraRangeStart: cameraRange['start'] as int? ?? 0,
      cameraRangeEnd: cameraRange['end'] as int? ?? 0,
      monitorRangeStart: monitorRange['start'] as int? ?? 0,
      monitorRangeEnd: monitorRange['end'] as int? ?? 0,
    );
  }

  @override
  List<Object?> get props => [
        id,
        name,
        type,
        enabled,
        address,
        port,
        username,
        bridgeUrl,
        websocketUrl,
        cameraRangeStart,
        cameraRangeEnd,
        monitorRangeStart,
        monitorRangeEnd,
      ];
}
