import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'package:worker/core/router/app_router.dart';
import 'package:worker/core/theme/app_theme.dart';
import 'package:worker/core/theme/theme_provider.dart';
import 'package:worker/generated/l10n/app_localizations.dart';

/// Root application widget for Worker Mobile App
///
/// This is the main app widget that:
/// - Sets up Material 3 theme configuration
/// - Configures localization for Vietnamese and English
/// - Provides router configuration (to be implemented)
/// - Integrates with Riverpod for state management
/// - Handles app-level error states
class WorkerApp extends ConsumerWidget {
  const WorkerApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // Watch router provider to get auth-aware router
    final router = ref.watch(routerProvider);

    // Watch theme settings for dynamic theming
    final themeSettings = ref.watch(themeSettingsProvider);

    return MaterialApp.router(
      // ==================== App Configuration ====================
      debugShowCheckedModeBanner: false,
      title: 'DBIZ Partner',

      // ==================== Router Configuration ====================
      // Using go_router for declarative routing with deep linking support
      // Router is provided by Riverpod and includes auth state management
      routerConfig: router,

      // ==================== Theme Configuration ====================
      // Material 3 theme with dynamic seed color from settings
      theme: AppTheme.lightTheme(themeSettings.seedColor),
      darkTheme: AppTheme.darkTheme(themeSettings.seedColor),
      themeMode: themeSettings.themeMode,
      // ==================== Localization Configuration ====================
      // Support for Vietnamese (primary) and English (secondary)
      localizationsDelegates: const [
        // App-specific localizations
        AppLocalizations.delegate,

        // Material Design localizations
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],

      // Supported locales
      supportedLocales: const [
        Locale('vi', 'VN'), // Vietnamese (primary)
        Locale('en', 'US'), // English (secondary)
      ],

      // Default locale (Vietnamese)
      locale: const Locale(
        'vi',
        'VN',
      ), // TODO: Make this configurable from settings
      // Locale resolution strategy
      localeResolutionCallback: (locale, supportedLocales) {
        // Check if the device locale is supported
        for (final supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale?.languageCode) {
            return supportedLocale;
          }
        }

        // If device locale is not supported, default to Vietnamese
        return const Locale('vi', 'VN');
      },

      // ==================== Material App Configuration ====================
      // Builder for additional context-dependent widgets
      builder: (context, child) {
        return _AppBuilder(child: child ?? const SizedBox.shrink());
      },
    );
  }
}

/// App builder widget
///
/// Wraps the entire app with additional widgets:
/// - Error boundary
/// - Connectivity listener
/// - Global overlays (loading, snackbars)
class _AppBuilder extends ConsumerWidget {
  const _AppBuilder({required this.child});

  final Widget child;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // TODO: Add connectivity listener
    // final connectivity = ref.watch(connectivityProvider);

    // TODO: Add global loading state
    // final isLoading = ref.watch(globalLoadingProvider);

    return Stack(
      children: [
        // Main app content
        child,

        // TODO: Add global loading overlay
        // if (isLoading)
        //   const _GlobalLoadingOverlay(),

        // TODO: Add connectivity banner
        // if (connectivity == ConnectivityStatus.offline)
        //   const _OfflineBanner(),
      ],
    );
  }
}
