import 'package:adaptix/adaptix.dart';
import 'package:flutter/material.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:provider/provider.dart';
import 'package:sapaly_shop/features/screens/category/category_screen.dart';
import 'package:sapaly_shop/features/screens/dashboard/dashboard.dart';
import 'package:sapaly_shop/features/screens/drawer/sapaly_drawer.dart';
import 'package:sapaly_shop/features/screens/home/home_screen.dart';
import 'package:sapaly_shop/features/screens/auth/login/login_screen.dart';
import 'package:sapaly_shop/features/services/auth_service.dart';
import 'package:sapaly_shop/providers/user_provider.dart';
import 'package:sapaly_shop/router.dart';
import 'package:sapaly_shop/themes/app_theme.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'constants/global_variables.dart';
import 'models/settings_model.dart';

Future<void> main() async {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);

  SharedPreferences prefs = await SharedPreferences.getInstance();
  await SettingsModel.initLocalization(
      prefs.getString('language') ?? kDefaultLanguage);
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => SettingsModel(prefs)),
        ChangeNotifierProvider(create: (_) => UserProvider()),
      ],
      child: const MyApp(),
    ),
  );
  FlutterNativeSplash.remove();
}

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

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

class _MyAppState extends State<MyApp> {
  final AuthService service = AuthService();

  @override
  Widget build(BuildContext context) {
    return SizeInitializer(
      builder: (context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: AppTheme.appLightTheme,
          title: 'Sapaly Mahabat',
          initialRoute: '/login',
          onGenerateRoute: (settings) => generateRoute(settings),
          routes: <String, WidgetBuilder>{
            '/': (context) => const Dashboard(),
            '/drawer': (context) => SapalyDrawer(),
            '/home': (context) => const HomeScreen(),
            '/category': (context) => const CategoryScreen(),
            '/login': (context) => LoginScreen(),
          },
          localizationsDelegates: GlobalVariables.localizationsDelegate,
          supportedLocales: GlobalVariables.supportedLocales,
        );
      },
    );
  }
}
