import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'core/app_colors.dart';
import 'services/auth_service.dart';
import 'services/route_simulator_service.dart';
import 'services/theme_service.dart';
import 'screens/splash_screen.dart';
import 'screens/login_screen.dart';
import 'screens/register_screen.dart';
import 'screens/citizen/citizen_home_screen.dart';
import 'screens/driver/driver_home_screen.dart';
import 'screens/admin/admin_dashboard_screen.dart';
import 'screens/onboarding/onboarding_screen.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final prefs = await SharedPreferences.getInstance();
  final onboardingDone = prefs.getBool('onboarding_done') ?? false;
  runApp(CelayaLimpiaApp(onboardingDone: onboardingDone));
}

class CelayaLimpiaApp extends StatelessWidget {
  final bool onboardingDone;
  const CelayaLimpiaApp({super.key, required this.onboardingDone});

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => AuthService()),
        ChangeNotifierProvider(create: (_) => RouteSimulatorService()),
        ChangeNotifierProvider(create: (_) => ThemeService()),
      ],
      child: Consumer<ThemeService>(
        builder: (_, themeService, __) => MaterialApp(
          title: 'Celaya Limpia',
          debugShowCheckedModeBanner: false,
          themeMode: themeService.themeMode,
          theme: _lightTheme(),
          darkTheme: _darkTheme(),
          initialRoute: onboardingDone ? '/splash' : '/onboarding',
          routes: {
            '/onboarding': (_) => const OnboardingScreen(),
            '/splash':     (_) => const SplashScreen(),
            '/login':      (_) => const LoginScreen(),
            '/register':   (_) => const RegisterScreen(),
            '/home':       (_) => const CitizenHomeScreen(),
            '/driver':     (_) => const DriverHomeScreen(),
            '/admin':      (_) => const AdminDashboardScreen(),
          },
        ),
      ),
    );
  }

  ThemeData _lightTheme() => ThemeData(
    useMaterial3: true,
    brightness: Brightness.light,
    colorScheme: ColorScheme.fromSeed(seedColor: AppColors.guindaPrimary,
        primary: AppColors.guindaPrimary, secondary: AppColors.dorado),
    scaffoldBackgroundColor: AppColors.grisFondo,
    inputDecorationTheme: const InputDecorationTheme(
      focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(color: AppColors.guindaPrimary, width: 2)),
      labelStyle: TextStyle(color: AppColors.guindaPrimary)),
  );

  ThemeData _darkTheme() => ThemeData(
    useMaterial3: true,
    brightness: Brightness.dark,
    colorScheme: ColorScheme.fromSeed(seedColor: AppColors.guindaLight,
        brightness: Brightness.dark, primary: AppColors.guindaLight,
        secondary: AppColors.dorado),
    scaffoldBackgroundColor: const Color(0xFF121212),
    cardColor: const Color(0xFF1E1E1E),
    inputDecorationTheme: const InputDecorationTheme(
      focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(color: AppColors.guindaLight, width: 2)),
      labelStyle: TextStyle(color: AppColors.guindaLight)),
  );
}
