import 'package:client_authentication/client_authentication.dart'
    as client_authentication;
import 'package:client_create_order/client_create_order.dart'
    as client_create_order;
import 'package:client_hubs/client_hubs.dart' as client_hubs;
import 'package:client_main/client_main.dart' as client_main;
import 'package:client_settings/client_settings.dart' as client_settings;
import 'package:core_localization/core_localization.dart' as core_localization;
import 'package:design_system/design_system.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:krow_core/core.dart';

import 'firebase_options.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: kIsWeb ? DefaultFirebaseOptions.currentPlatform : null,
  );
  
  // Register global BLoC observer for centralized error logging
  Bloc.observer = CoreBlocObserver(
    logEvents: true,
    logStateChanges: false, // Set to true for verbose debugging
  );
  
  runApp(ModularApp(module: AppModule(), child: const AppWidget()));
}

/// The main application module for the Client app.
class AppModule extends Module {
  @override
  List<Module> get imports => <Module>[core_localization.LocalizationModule()];

  @override
  void routes(RouteManager r) {
    // Initial route points to the client authentication flow
    r.module(ClientPaths.root, module: client_authentication.ClientAuthenticationModule());

    // Client main shell with bottom navigation (includes home as a child)
    r.module(ClientPaths.main, module: client_main.ClientMainModule());

    // Client settings route
    r.module(
      ClientPaths.settings,
      module: client_settings.ClientSettingsModule(),
    );

    // Client hubs route
    r.module(ClientPaths.hubs, module: client_hubs.ClientHubsModule());

    // Client create order route
    r.module(
      ClientPaths.createOrder,
      module: client_create_order.ClientCreateOrderModule(),
    );
  }
}

class AppWidget extends StatelessWidget {
  const AppWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return WebMobileFrame(
      appName: 'KROW Client\nApplication',
      logo: Image.asset('assets/logo.png'),
      child: BlocProvider<core_localization.LocaleBloc>(
        create: (BuildContext context) =>
            Modular.get<core_localization.LocaleBloc>(),
        child:
            BlocBuilder<
              core_localization.LocaleBloc,
              core_localization.LocaleState
            >(
              builder:
                  (BuildContext context, core_localization.LocaleState state) {
                    return core_localization.TranslationProvider(
                      child: MaterialApp.router(
                        debugShowCheckedModeBanner: false,
                        title: "Krow Client",
                        theme: UiTheme.light,
                        routerConfig: Modular.routerConfig,
                        locale: state.locale,
                        supportedLocales: state.supportedLocales,
                        localizationsDelegates:
                            const <LocalizationsDelegate<dynamic>>[
                              GlobalMaterialLocalizations.delegate,
                              GlobalWidgetsLocalizations.delegate,
                              GlobalCupertinoLocalizations.delegate,
                            ],
                      ),
                    );
                  },
            ),
      ),
    );
  }
}
