import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:proxibuy/core/routes/app_router.dart';
import 'package:proxibuy/data/storage/shared_preferences_helper.dart';
import 'package:proxibuy/presentation/providers/cubit/them_mode_cubit.dart';
import 'package:proxibuy/presentation/providers/cubit/user_info_cubit.dart';
import 'package:proxibuy/presentation/ui/theme/theme.dart';
import 'package:url_strategy/url_strategy.dart';

void main() async {
  setPathUrlStrategy(); // 👈 Removes the # from URLs
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
  await SharedPreferencesHelper.initial();

  runApp(MultiBlocProvider(
    providers: [
      BlocProvider<ThemModeCubit>(create: (context) => ThemModeCubit()),
      BlocProvider<UserInfoCubit>(
          create: (context) => UserInfoCubit()..getUserInfo()),
    ],
    child: const MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    FlutterNativeSplash.remove();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ThemModeCubit, ThemeMode>(
      builder: (context, themeState) {
        return MaterialApp.router(
          routerConfig: AppRouter().createRouter,
          title: 'Proxibuy',
          theme: appTheme,
          darkTheme: darkTheme,
          themeMode: themeState,
          debugShowCheckedModeBanner: false,
        );
      },
    );
  }
}
