import 'dart:io';

/// Locates `status.sh` by walking up from the current working directory to
/// the repo root (marked by `.git`), then descending a fixed path — robust
/// regardless of which subdirectory `flutter run` happens to be invoked
/// from, unlike a hardcoded relative path.
String locateStatusScript() {
  var dir = Directory.current;
  while (!Directory('${dir.path}/.git').existsSync()) {
    final parent = dir.parent;
    if (parent.path == dir.path) {
      throw StateError(
        'Could not locate the repo root (.git) from ${Directory.current.path}. '
        'Run mam_desktop from within the multi-agent-mux repo checkout.',
      );
    }
    dir = parent;
  }

  final scriptPath =
      '${dir.path}/.agents/skills/multi-agent-mux-status/scripts/status.sh';
  if (!File(scriptPath).existsSync()) {
    throw StateError('status.sh not found at $scriptPath');
  }
  return scriptPath;
}
