import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_fullscreen/flutter_fullscreen.dart';
import 'package:hive/hive.dart';
import 'package:logging/logging.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdfrx/pdfrx.dart';

import 'app.dart';

/// Application entry point.
///
/// Performs platform-specific initialization before running the app
Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await _initializeLogging();
  await _initializePlugins();

  runApp(const SheetlessApp());
}

/// Configures the logging system.
Future<void> _initializeLogging() async {
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((record) {
    debugPrint('${record.level.name}: ${record.time}: ${record.message}');
    if (record.error != null) {
      debugPrint('${record.error}');
    }
  });
}

/// Initializes platform-specific plugins and services.
Future<void> _initializePlugins() async {
  // Fullscreen support
  await FullScreen.ensureInitialized();

  // PDF rendering (especially needed for web)
  pdfrxFlutterInitialize();

  // Local storage (not supported or necessary on web)
  if (!kIsWeb) {
    final dir = await getApplicationDocumentsDirectory();
    Hive.init(dir.path);
  }
}
