/// A lightweight value object describing a cross-feature navigation handoff.
///
/// Carries the destination route and optional arguments (e.g. a selected-item
/// ID or a preset filter) so the target page can open in the right state.
///
/// This lives in the app layer (`kell_web`) and is never imported by feature
/// packages, keeping package boundaries strict.
class NavigationTarget {
  /// The route path to navigate to (one of [AppRoutes] constants).
  final String route;

  /// Optional key-value arguments for the destination page.
  ///
  /// Common keys:
  /// - `selectedSku` – pre-select an item by SKU on the target page
  /// - `selectedId`  – pre-select an item by ID on the target page
  /// - `filter`      – apply a preset filter (e.g. `'lowStock'`)
  /// - `category`    – filter by policy category
  final Map<String, String> arguments;

  const NavigationTarget({required this.route, this.arguments = const {}});

  /// Convenience: the selected SKU, if any.
  String? get selectedSku => arguments['selectedSku'];

  /// Convenience: the selected ID, if any.
  String? get selectedId => arguments['selectedId'];

  /// Convenience: a preset filter, if any.
  String? get filter => arguments['filter'];

  /// Convenience: a category filter, if any.
  String? get category => arguments['category'];

  /// Convenience: a search query, if any.
  String? get query => arguments['query'];

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is NavigationTarget && route == other.route && _mapsEqual(arguments, other.arguments);

  @override
  int get hashCode {
    // Build a stable hash from sorted keys to ensure consistency.
    var argsHash = 0;
    final sortedKeys = arguments.keys.toList()..sort();
    for (final key in sortedKeys) {
      argsHash = argsHash ^ Object.hash(key, arguments[key]);
    }
    return Object.hash(route, argsHash);
  }

  @override
  String toString() => 'NavigationTarget(route: $route, arguments: $arguments)';

  static bool _mapsEqual(Map<String, String> a, Map<String, String> b) {
    if (a.length != b.length) return false;
    for (final key in a.keys) {
      if (a[key] != b[key]) return false;
    }
    return true;
  }
}
