import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'core/theme.dart';
import 'core/database.dart';
import 'core/preferences.dart';
import 'core/l10n.dart';
import 'screens/trip_list_screen.dart';
import 'screens/trip_create_screen.dart';
import 'screens/trip_active_screen.dart';
import 'screens/trip_detail_screen.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // Initialize Shared Preferences & UUID identity keys
  await AppPreferences.init();

  // Trigger DB opening to ensure database is created/ready
  await DatabaseHelper.instance.database;

  runApp(const LiveVacationMemoriesApp());
}

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

  @override
  State<LiveVacationMemoriesApp> createState() => _LiveVacationMemoriesAppState();
}

class _LiveVacationMemoriesAppState extends State<LiveVacationMemoriesApp> {
  // GoRouter routing configuration
  final GoRouter _router = GoRouter(
    initialLocation: '/',
    routes: [
      GoRoute(
        path: '/',
        builder: (context, state) => const TripListScreen(),
      ),
      GoRoute(
        path: '/create',
        builder: (context, state) => const TripCreateScreen(),
      ),
      GoRoute(
        path: '/active/:id',
        builder: (context, state) {
          final id = state.pathParameters['id']!;
          return TripActiveScreen(tripId: id);
        },
      ),
      GoRoute(
        path: '/detail/:id',
        builder: (context, state) {
          final id = state.pathParameters['id']!;
          return TripDetailScreen(tripId: id);
        },
      ),
    ],
  );

  @override
  Widget build(BuildContext context) {
    // Listen to changes in preferences (themeMode and language)
    return ValueListenableBuilder<String>(
      valueListenable: ThemeNotifier.themeModeListener,
      builder: (context, themeMode, child) {
        return ValueListenableBuilder<String>(
          valueListenable: LanguageNotifier.languageListener,
          builder: (context, language, child) {
            return MaterialApp.router(
              title: L10n.appTitle,
              routerConfig: _router,
              theme: buildLightTheme(),
              darkTheme: buildDarkTheme(),
              themeMode: themeMode == 'light' ? ThemeMode.light : ThemeMode.dark,
              debugShowCheckedModeBanner: false,
            );
          },
        );
      },
    );
  }
}

// Simple notifier to trigger redraw of the app when theme mode is changed in settings
class ThemeNotifier {
  static final ValueNotifier<String> themeModeListener = ValueNotifier(AppPreferences.themeMode);

  static void updateTheme(String nextTheme) {
    themeModeListener.value = nextTheme;
  }
}

// Simple notifier to trigger redraw of the app when language is changed
class LanguageNotifier {
  static final ValueNotifier<String> languageListener = ValueNotifier(AppPreferences.language);

  static void updateLanguage(String nextLang) {
    languageListener.value = nextLang;
  }
}
