import 'package:feature_inventory/feature_inventory.dart';
import 'package:feature_orders/feature_orders.dart';
import 'package:feature_policy/feature_policy.dart';
import 'package:feature_wordpress/feature_wordpress.dart';
import 'package:flutter/material.dart';

import '../composition/app_scope.dart';
import '../dashboard/application/dashboard_controller.dart';
import '../dashboard/application/get_dashboard_summary.dart';
import '../navigation/app_navigation.dart';
import '../pages/dashboard_page.dart';
import '../pages/finance_placeholder_page.dart';
import '../pages/integrations_placeholder_page.dart';
import '../shell/app_shell.dart';

abstract final class AppRoutes {
  static const String dashboard = '/';
  static const String inventory = '/inventory';
  static const String products = '/products';
  static const String orders = '/orders';
  static const String finance = '/finance';
  static const String policy = '/policy';
  static const String integrations = '/integrations';

  static Route<dynamic> onGenerateRoute(RouteSettings settings) {
    // Extract navigation arguments passed via NavigationTarget.
    final args = _extractArgs(settings);

    switch (settings.name) {
      case dashboard:
        return _buildRoute(settings, (context) {
          final services = AppScope.of(context);
          final controller = DashboardController(
            GetDashboardSummary(
              inventoryRepository: services.inventoryRepository,
              productPublishingRepository: services.productPublishingRepository,
              ordersRepository: services.ordersRepository,
            ),
          );
          return AppShell(
            selectedRoute: dashboard,
            title: 'Dashboard',
            child: DashboardPage(controller: controller),
          );
        });
      case inventory:
        return _buildRoute(
          settings,
          (context) => AppShell(
            selectedRoute: inventory,
            title: 'Inventory',
            child: InventoryPage(
              repository: AppScope.of(context).inventoryRepository,
              onViewProduct: (sku) => AppNavigation.inventoryToProduct(context, sku: sku),
              initialFilter: args['filter'],
              initialQuery: args['query'],
              initialSelectedSku: args['selectedSku'],
            ),
          ),
        );
      case products:
        return _buildRoute(
          settings,
          (context) => AppShell(
            selectedRoute: products,
            title: 'Products',
            child: ProductPublishingPage(
              repository: AppScope.of(context).productPublishingRepository,
              onViewPolicy: () => AppNavigation.productToPolicy(context),
              initialFilter: args['filter'],
              initialQuery: args['query'],
              initialSelectedSku: args['selectedSku'],
            ),
          ),
        );
      case orders:
        return _buildRoute(
          settings,
          (context) => AppShell(
            selectedRoute: orders,
            title: 'Orders',
            child: OrdersPage(
              repository: AppScope.of(context).ordersRepository,
              onViewProduct: (sku) => AppNavigation.orderToProduct(context, sku: sku),
              onViewInventory: (sku) => AppNavigation.orderToInventory(context, sku: sku),
              initialFilter: args['filter'],
              initialQuery: args['query'],
              initialSelectedId: args['selectedId'],
            ),
          ),
        );
      case finance:
        return _buildRoute(
          settings,
          (context) => const AppShell(
            selectedRoute: finance,
            title: 'Finance',
            child: FinancePlaceholderPage(),
          ),
        );
      case policy:
        return _buildRoute(
          settings,
          (context) => AppShell(
            selectedRoute: policy,
            title: 'Policy',
            child: PolicyPage(
              repository: AppScope.of(context).policyRepository,
              onViewRelatedPage: (category) =>
                  AppNavigation.policyToRelatedPage(context, category: category),
              initialCategory: args['category'],
              initialQuery: args['query'],
              initialSelectedId: args['selectedId'],
            ),
          ),
        );
      case integrations:
        return _buildRoute(
          settings,
          (context) => const AppShell(
            selectedRoute: integrations,
            title: 'Integrations',
            child: IntegrationsPlaceholderPage(),
          ),
        );
      default:
        return _buildRoute(
          settings,
          (context) => AppShell(
            selectedRoute: inventory,
            title: 'Inventory',
            child: InventoryPage(
              repository: AppScope.of(context).inventoryRepository,
              onViewProduct: (sku) => AppNavigation.inventoryToProduct(context, sku: sku),
            ),
          ),
        );
    }
  }

  static MaterialPageRoute<dynamic> _buildRoute(
    RouteSettings settings,
    Widget Function(BuildContext context) pageBuilder,
  ) {
    return MaterialPageRoute<dynamic>(settings: settings, builder: pageBuilder);
  }

  /// Safely extracts the `Map<String, String>` arguments from [RouteSettings].
  ///
  /// Returns an empty map when no arguments are present or the type doesn't
  /// match, so callers can always index safely.
  static Map<String, String> _extractArgs(RouteSettings settings) {
    final raw = settings.arguments;
    if (raw is Map<String, String>) return raw;
    return const {};
  }
}
