/// Sequence definition loaded from the coordinator.
class SequenceDefinition {
  final int id;
  final String name;
  final int categoryId;
  final List<int> cameras;
  final int intervalSeconds;

  const SequenceDefinition({
    required this.id,
    required this.name,
    required this.categoryId,
    required this.cameras,
    required this.intervalSeconds,
  });

  factory SequenceDefinition.fromJson(Map<String, dynamic> json) {
    return SequenceDefinition(
      id: json['id'] as int? ?? json['Id'] as int? ?? 0,
      name: json['name'] as String? ?? json['Name'] as String? ?? '',
      categoryId:
          json['categoryId'] as int? ?? json['CategoryId'] as int? ?? 0,
      cameras: (json['cameras'] as List<dynamic>? ??
              json['Cameras'] as List<dynamic>? ??
              [])
          .cast<int>(),
      intervalSeconds: json['intervalSeconds'] as int? ??
          json['IntervalSeconds'] as int? ??
          5,
    );
  }
}

/// Sequence category for grouping sequences.
class SequenceCategory {
  final int id;
  final String name;

  const SequenceCategory({required this.id, required this.name});

  factory SequenceCategory.fromJson(Map<String, dynamic> json) {
    return SequenceCategory(
      id: json['id'] as int? ?? json['Id'] as int? ?? 0,
      name: json['name'] as String? ?? json['Name'] as String? ?? '',
    );
  }
}

/// A sequence currently running on a viewer.
class RunningSequence {
  final int viewerId;
  final int sequenceId;
  final DateTime startedAt;

  const RunningSequence({
    required this.viewerId,
    required this.sequenceId,
    required this.startedAt,
  });

  factory RunningSequence.fromJson(Map<String, dynamic> json) {
    return RunningSequence(
      viewerId: json['viewerId'] as int? ?? json['ViewerId'] as int? ?? 0,
      sequenceId:
          json['sequenceId'] as int? ?? json['SequenceId'] as int? ?? 0,
      startedAt: DateTime.tryParse(json['startedAt'] as String? ??
              json['StartedAt'] as String? ??
              '') ??
          DateTime.now(),
    );
  }
}
