import 'package:conalep_pos/viewmodels/corte_caja_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'dart:io'; // Importa esto para usar Platform.isWindows

import 'services/productos_service.dart';
import 'views/main/main_screen.dart';
import 'views/home/home_screen.dart';
import 'views/login/login_screen.dart';
import 'views/perfil/perfil_screen.dart';
import 'themes/themes.dart';
import 'viewmodels/viewmodels.dart';
import 'package:timezone/data/latest.dart' as tzdata;

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

  // Inicialización de la base de datos para plataformas de escritorio con FFI
  if (Platform.isWindows || Platform.isLinux) {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
  }

  final productosService = ProductosService();
  if (await productosService.isDatabaseEmpty()) {
    print("La base de datos está vacía. Llenando...");
    productosService.fillCategoriaBD();
    productosService.fillProductoBD();
  } else {
    print("La base de datos ya contiene datos.");
  }

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeRight,
    DeviceOrientation.landscapeLeft,
  ]).then((_) {
    runApp(MultiProvider(providers: [
      ChangeNotifierProvider(create: (_) => LoginViewModel()),
      ChangeNotifierProvider(create: (_) => UsuariosViewModel()),
      ChangeNotifierProvider(create: (_) => ProfileViewModel()),
      ChangeNotifierProvider(create: (_) => CategoriaProductoViewModel()),
      ChangeNotifierProvider(create: (_) => ProductoViewModel()),
      ChangeNotifierProvider(create: (_) => MediaViewModel()),
      ChangeNotifierProvider(create: (_) => PedidoViewModel()),
      ChangeNotifierProvider(create: (_) => CorteCajaViewModel()),
      ChangeNotifierProvider(create: (_) => DescuentoViewModel()),
      ChangeNotifierProvider(create: (_) => VariableViewModel()),
      // Agrega aquí cualquier otro provider que necesites
    ], child: const MyApp()));
  });
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      locale: const Locale("en", "ES"),
      debugShowCheckedModeBanner: false,
      title: 'Conalep POS',
      theme: AppTheme.lightTheme,
      initialRoute: 'main',
      routes: {
        'main': (context) => const MainScreen(),
        'login': (context) => const LoginScreen(),
        'home': (context) => const HomeScreen(),
        'perfil': (context) => const PerfilScreen(),
      },
    );
  }
}
