import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mam_core/mam_core.dart';

import '../status_script_locator.dart';

/// The single [SessionService] instance for this app run — owns the
/// resolved path to `status.sh`.
final sessionServiceProvider = Provider<SessionService>((ref) {
  return SessionService(statusScriptPath: locateStatusScript());
});

/// Polling + stale/backoff (D6) sits in `mam_core`; this provider just wires
/// it up. `apps/mam_desktop` does not reimplement the timer/backoff logic.
final statusRepositoryProvider = Provider<StatusRepository>((ref) {
  return StatusRepository(sessionService: ref.watch(sessionServiceProvider));
});

/// Reactive stream of [SessionsPoll] the UI subscribes to. 4s base interval
/// on success, backing off to 6s/15s on repeated `status.sh` failures.
final sessionsPollProvider = StreamProvider<SessionsPoll>((ref) {
  final repo = ref.watch(statusRepositoryProvider);
  return repo.watch();
});

/// Currently selected row name for the Master-Detail layout.
final selectedSessionNameProvider = StateProvider<String?>((ref) => null);
