import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import 'core/sacred_tokens.dart';
import 'data/local/models.dart';
import 'features/home/home_view.dart';

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

  // Landscape-only for TV
  await SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight,
  ]);

  // Hide system overlays for full-screen kiosk mode
  await SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);

  // Initialize Hive
  await Hive.initFlutter();
  Hive.registerAdapter(AppSettingsAdapter());
  Hive.registerAdapter(DailyPrayerScheduleAdapter());
  await Hive.openBox<AppSettings>(HiveBoxes.settings);
  await Hive.openBox<DailyPrayerSchedule>(HiveBoxes.prayerSchedule);

  // Seed defaults if first launch
  final settingsBox = Hive.box<AppSettings>(HiveBoxes.settings);
  if (settingsBox.get('default') == null) {
    await settingsBox.put('default', AppSettings());
  }

  // Initialize date formatting for Indonesian locale
  await initializeDateFormatting('id_ID');

  // Keep screen awake — CRITICAL for 24/7 TV operation
  await WakelockPlus.enable();

  runApp(
    const ProviderScope(
      child: JamShalatApp(),
    ),
  );
}

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

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // textScaleProvider will be used selectively in child components.

    return MaterialApp(
      title: 'Jam Shalat Digital',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        useMaterial3: true,
        fontFamily: 'Manrope',
        scaffoldBackgroundColor: SacredColors.background,
        colorScheme: const ColorScheme.dark(
          primary: SacredColors.primary,
          secondary: SacredColors.secondary,
          surface: SacredColors.surface,
          error: SacredColors.error,
          onPrimary: SacredColors.onPrimary,
          onSecondary: SacredColors.onSecondary,
          onSurface: SacredColors.onSurface,
          onError: Color(0xFF690005),
        ),
      ),
      home: const HomeView(),
    );
  }
}
