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

import 'theme/app_theme.dart';

import 'screens/onboarding/onboarding_screen.dart';
import 'screens/home/home_screen.dart';
import 'screens/husband/husband_home_screen.dart';
import 'models/user_profile.dart';
import 'models/cycle_entry.dart';
import 'models/teaching_plan.dart';
import 'models/scripture.dart';
import 'providers/user_provider.dart';
import 'app_startup.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());
  Hive.registerAdapter(SupplyItemAdapter());
  Hive.registerAdapter(PadTypeAdapter());
  Hive.registerAdapter(TeachingPlanAdapter());

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

// 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) {
    // Watch user profile to determine
    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;

        case AppThemeMode.system:
          themeMode = ThemeMode.system;
          break;
      }
    } else {
      // Default theme for initial load or if profile is null
      themeMode = ThemeMode.system;
      accentColor = AppColors.sageGreen;
    }

    // Determine the home screen based on profile state
    Widget homeScreen;
    if (userProfile == null) {
      homeScreen = const OnboardingScreen();
    } else if (userProfile.role == UserRole.husband) {
      homeScreen = const HusbandHomeScreen();
    } else {
      homeScreen = const HomeScreen();
    }

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