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/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:staff_authentication/staff_authentication.dart'
    as staff_authentication;
import 'package:staff_main/staff_main.dart' as staff_main;

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(ModularApp(module: AppModule(), child: const AppWidget()));
}

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

  @override
  void routes(RouteManager r) {
    // Set the initial route to the authentication module
    r.module("/", module: staff_authentication.StaffAuthenticationModule());

    r.module('/worker-main', module: staff_main.StaffMainModule());
  }
}

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

  @override
  Widget build(BuildContext context) {
    return 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(
                      title: "KROW Staff",
                      theme: UiTheme.light,
                      routerConfig: Modular.routerConfig,
                      locale: state.locale,
                      supportedLocales: state.supportedLocales,
                      localizationsDelegates:
                          const <LocalizationsDelegate<dynamic>>[
                            GlobalMaterialLocalizations.delegate,
                            GlobalWidgetsLocalizations.delegate,
                            GlobalCupertinoLocalizations.delegate,
                          ],
                    ),
                  );
                },
          ),
    );
  }
}
