import 'package:digiplug/core/navigation/navigator.dart';
import 'package:digiplug/data/repositories/auth_repository.dart';
import 'package:digiplug/features/login/presentation/views/splash_screen.dart';
import 'package:digiplug/providers/home_data_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:provider/provider.dart';
import 'package:digiplug/bat_theme/bat_theme.dart';

// Import untuk lokalisasi
import 'package:flutter_localizations/flutter_localizations.dart';

// ThemeProvider untuk mengelola mode terang/gelap
class ThemeProvider extends ChangeNotifier {
  ThemeMode _theme = ThemeMode.dark;
  ThemeMode get theme => _theme;
  bool get isDark => _theme == ThemeMode.dark;

  void changeMode() {
    _theme = isDark ? ThemeMode.light : ThemeMode.dark;
    SystemChrome.setSystemUIOverlayStyle(
      isDark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark,
    );
    notifyListeners();
  }
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initializeDateFormatting('id_ID', null);
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  runApp(const AppState());
}

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

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        // Provider untuk tema
        ChangeNotifierProvider(create: (_) => ThemeProvider()),
        // Provider untuk logika otentikasi lokal
        Provider(create: (_) => AuthRepository()),
        // Provider untuk data statis di halaman home
        ChangeNotifierProvider(create: (_) => HomeDataProvider()),
      ],
      child: const MyApp(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Consumer<ThemeProvider>(
      builder: (context, themeProvider, child) {
        return ScreenUtilInit(
          designSize: const Size(428, 926),
          minTextAdapt: true,
          builder: (context, child) {
            return BatCave(
              debugShowCheckedModeBanner: false,
              title: 'DigiHome Simple',
              themeMode: themeProvider.theme,
              theme: const BatThemeData.light(),
              darkTheme: const BatThemeData.dark(),

              // --- PERBAIKAN DI SINI ---
              // Daftarkan delegasi bahasa yang didukung
              localizationsDelegates: const [
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
              ],
              // Parameter 'supportedLocales' dihapus karena tidak ada di constructor BatCave
              // --- AKHIR PERBAIKAN ---

              home: const SplashScreen(), // Mulai dari SplashScreen
              onGenerateRoute: AppRouter.generateRoutes,
              navigatorKey: AppNavigator.key,
            );
          },
        );
      },
    );
  }
}
