import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hive_flutter/hive_flutter.dart';

import 'models/scripture.dart';
import 'theme/app_theme.dart';
import 'screens/splash_screen.dart';
import 'models/user_profile.dart';
import 'models/cycle_entry.dart';
import 'providers/user_provider.dart';
import 'services/notification_service.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Initialize Hive for local storage
  await Hive.initFlutter();

  // Register Hive adapters
  Hive.registerAdapter(UserProfileAdapter());
  Hive.registerAdapter(CycleEntryAdapter());
  Hive.registerAdapter(RelationshipStatusAdapter());
  Hive.registerAdapter(FertilityGoalAdapter());
  Hive.registerAdapter(MoodLevelAdapter());
  Hive.registerAdapter(FlowIntensityAdapter());
  Hive.registerAdapter(CervicalMucusTypeAdapter());
  Hive.registerAdapter(CyclePhaseAdapter());
  Hive.registerAdapter(UserRoleAdapter());
  Hive.registerAdapter(BibleTranslationAdapter());
  Hive.registerAdapter(ScriptureAdapter());
  Hive.registerAdapter(AppThemeModeAdapter()); // Register new adapter
  Hive.registerAdapter(SupplyItemAdapter());
  Hive.registerAdapter(PadTypeAdapter());

  // Open boxes and load scriptures in parallel
  await Future.wait([
    Hive.openBox<UserProfile>('user_profile'),
    Hive.openBox<CycleEntry>('cycle_entries'),
    ScriptureDatabase().loadScriptures(),
  ]);

  // Initialize notifications
  await NotificationService().initialize();

  runApp(const ProviderScope(child: ChristianPeriodTrackerApp()));
}


// Helper to convert hex string to Color
Color _colorFromHex(String hexColor) {
  try {
    final hexCode = hexColor.replaceAll('0x', '');
    return Color(int.parse('FF$hexCode', radix: 16));
  } catch (e) {
    return AppColors.sageGreen; // Fallback to default
  }
}

class ChristianPeriodTrackerApp extends ConsumerWidget {
  const ChristianPeriodTrackerApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final userProfile = ref.watch(userProfileProvider);

    final ThemeMode themeMode;
    final Color accentColor;

    if (userProfile != null) {
      accentColor = _colorFromHex(userProfile.accentColor);
      switch (userProfile.themeMode) {
        case AppThemeMode.light:
          themeMode = ThemeMode.light;
          break;
        case AppThemeMode.dark:
          themeMode = ThemeMode.dark;
          break;
        case AppThemeMode.system:
        default:
          themeMode = ThemeMode.system;
          break;
      }
    } else {
      // Default theme for initial load or if profile is null
      themeMode = ThemeMode.system;
      accentColor = AppColors.sageGreen;
    }

    return MaterialApp(
      title: 'Christian Period Tracker',
      debugShowCheckedModeBanner: false,
      theme: AppTheme.getLightTheme(accentColor),
      darkTheme: AppTheme.getDarkTheme(accentColor),
      themeMode: themeMode,
      home: const SplashScreen(),
    );
  }
}
