import 'package:flutter/widgets.dart';

import '../routing/app_routes.dart';
import 'navigation_target.dart';

/// App-level navigation helpers for cross-feature handoffs.
///
/// All navigation logic lives here in the app layer so that feature packages
/// never depend on each other's routes. Feature pages receive plain
/// `void Function(String)` callbacks that they invoke with a SKU or ID;
/// the wiring in [AppRoutes.onGenerateRoute] closes over these helpers.
abstract final class AppNavigation {
  // ── Generic ───────────────────────────────────────────────────────────

  /// Navigate to a [NavigationTarget], replacing the current route.
  static void navigateTo(BuildContext context, NavigationTarget target) {
    Navigator.of(context).pushReplacementNamed(target.route, arguments: target.arguments);
  }

  // ── Dashboard → feature handoffs ──────────────────────────────────────

  /// Dashboard KPI "Total Products" / "In Stock" → Inventory page.
  static void dashboardToInventory(BuildContext context, {String? filter, String? query}) {
    navigateTo(
      context,
      NavigationTarget(route: AppRoutes.inventory, arguments: {'filter': ?filter, 'query': ?query}),
    );
  }

  /// Dashboard KPI "Draft" → Products page.
  static void dashboardToProducts(BuildContext context, {String? filter, String? query}) {
    navigateTo(
      context,
      NavigationTarget(route: AppRoutes.products, arguments: {'filter': ?filter, 'query': ?query}),
    );
  }

  /// Dashboard KPI "Total Orders" / "Pending" / "Active" → Orders page.
  static void dashboardToOrders(BuildContext context, {String? filter, String? query}) {
    navigateTo(
      context,
      NavigationTarget(route: AppRoutes.orders, arguments: {'filter': ?filter, 'query': ?query}),
    );
  }

  /// Dashboard KPI "Revenue" → Finance page.
  static void dashboardToFinance(BuildContext context) {
    navigateTo(context, NavigationTarget(route: AppRoutes.finance));
  }

  // ── Policy detail → operational page handoffs ─────────────────────────

  /// Policy detail action → related operational page based on category.
  static void policyToRelatedPage(BuildContext context, {required String category}) {
    final route = _categoryToRoute(category);
    navigateTo(
      context,
      NavigationTarget(route: route, arguments: {'fromPolicy': 'true', 'category': category}),
    );
  }

  // ── Orders → Products / Inventory ─────────────────────────────────────

  /// Order line-item SKU → Products page, pre-selecting that SKU.
  static void orderToProduct(BuildContext context, {required String sku}) {
    navigateTo(
      context,
      NavigationTarget(route: AppRoutes.products, arguments: {'selectedSku': sku}),
    );
  }

  /// Order line-item SKU → Inventory page, pre-selecting that SKU.
  static void orderToInventory(BuildContext context, {required String sku}) {
    navigateTo(
      context,
      NavigationTarget(route: AppRoutes.inventory, arguments: {'selectedSku': sku}),
    );
  }

  // ── Products → Policy ─────────────────────────────────────────────────

  /// Product draft → Policy page filtered to "Product Compliance".
  static void productToPolicy(BuildContext context) {
    navigateTo(
      context,
      NavigationTarget(route: AppRoutes.policy, arguments: {'category': 'Product Compliance'}),
    );
  }

  // ── Inventory → Products ──────────────────────────────────────────────

  /// Inventory item → Products page, pre-selecting by SKU.
  static void inventoryToProduct(BuildContext context, {required String sku}) {
    navigateTo(
      context,
      NavigationTarget(route: AppRoutes.products, arguments: {'selectedSku': sku}),
    );
  }

  // ── Helpers ───────────────────────────────────────────────────────────

  static String _categoryToRoute(String category) {
    switch (category) {
      case 'Product Compliance':
        return AppRoutes.products;
      case 'Inventory Governance':
        return AppRoutes.inventory;
      case 'Order Operations':
        return AppRoutes.orders;
      case 'Finance & Tax':
        return AppRoutes.finance;
      case 'Customer Policy':
        return AppRoutes.orders;
      default:
        return AppRoutes.dashboard;
    }
  }
}
