import 'package:meta/meta.dart';

import 'drift_entry.dart';
import 'session_row.dart';

/// The full parsed result of one `status.sh --json` call: the untouched
/// pre-D8 keys (timestamp/yaml_path/tmux_sessions_alive/tmux_confirmed/
/// drifts/actions) plus the additive `sessions_detail` -> [SessionRow].
@immutable
class SessionsSnapshot {
  final DateTime timestamp;
  final String yamlPath;
  final List<String> tmuxSessionsAlive;
  final bool tmuxConfirmed;
  final List<DriftEntry> drifts;
  final List<String> actions;
  final List<SessionRow> sessions;

  const SessionsSnapshot({
    required this.timestamp,
    required this.yamlPath,
    required this.tmuxSessionsAlive,
    required this.tmuxConfirmed,
    required this.drifts,
    required this.actions,
    required this.sessions,
  });

  factory SessionsSnapshot.fromJson(Map<String, dynamic> json) {
    return SessionsSnapshot(
      timestamp:
          DateTime.tryParse(json['timestamp'] as String? ?? '')?.toUtc() ??
              DateTime.now().toUtc(),
      yamlPath: json['yaml_path'] as String? ?? '',
      tmuxSessionsAlive:
          (json['tmux_sessions_alive'] as List<dynamic>? ?? const [])
              .map((e) => e.toString())
              .toList(growable: false),
      tmuxConfirmed: json['tmux_confirmed'] as bool? ?? false,
      drifts: (json['drifts'] as List<dynamic>? ?? const [])
          .map((e) => DriftEntry.fromJson(e as Map<String, dynamic>))
          .toList(growable: false),
      actions: (json['actions'] as List<dynamic>? ?? const [])
          .map((e) => e.toString())
          .toList(growable: false),
      sessions: (json['sessions_detail'] as List<dynamic>? ?? const [])
          .map((e) => SessionRow.fromJson(e as Map<String, dynamic>))
          .toList(growable: false),
    );
  }
}
