import 'package:flutter/material.dart';
import 'package:my_attendance/screens/day_detail_screen.dart';
import 'package:my_attendance/screens/history_screen.dart';
import 'package:my_attendance/screens/home_screen.dart';
import 'package:my_attendance/screens/leave_request_screen.dart';
import 'package:my_attendance/screens/correction_screen.dart';
import 'package:my_attendance/services/attendance_service.dart';
import 'package:my_attendance/services/settings_service.dart';
import 'package:provider/provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi_web/sqflite_ffi_web.dart';
import 'package:flutter/foundation.dart';

void main() async {
  // Ensure that widgets binding is initialized before using any plugins
  WidgetsFlutterBinding.ensureInitialized();
  if (kIsWeb) {
    databaseFactory = databaseFactoryFfiWeb;
  }
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => SettingsService()),
        ChangeNotifierProvider(create: (_) => AttendanceService()),
      ],
      child: MaterialApp(
        title: 'Attendance App',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          useMaterial3: true,
          brightness: Brightness.light,
          colorSchemeSeed: Colors.deepPurple,
          appBarTheme: const AppBarTheme(centerTitle: true, elevation: 2),
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        darkTheme: ThemeData(
          useMaterial3: true,
          brightness: Brightness.dark,
          colorSchemeSeed: Colors.deepPurple,
          appBarTheme: const AppBarTheme(centerTitle: true),
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        themeMode: ThemeMode.system,
        home: const HomeScreen(),
        routes: {
          HistoryScreen.routeName: (ctx) => const HistoryScreen(),
          DayDetailScreen.routeName: (ctx) => const DayDetailScreen(),
          LeaveRequestScreen.routeName: (ctx) => const LeaveRequestScreen(),
          CorrectionScreen.routeName: (ctx) => const CorrectionScreen(),
        },
      ),
    );
  }
}
