import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:olivamia_pos/viewmodels/descuento_view_model.dart';
import 'package:provider/provider.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'dart:io';

import 'services/productos_service.dart';
import 'views/main/main_screen.dart';
import 'views/home/home_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();

  if (Platform.isWindows || Platform.isLinux) {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
  }

  final productosService = ProductosService();
  if (await productosService.isDatabaseEmpty()) {
    await productosService.fillCategoriaBD();
    await productosService.fillProductoBD();
  }

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeRight,
    DeviceOrientation.landscapeLeft,
  ]).then((_) {
    runApp(MultiProvider(providers: [
      ChangeNotifierProvider(create: (_) => CategoriaProductoViewModel()),
      ChangeNotifierProvider(create: (_) => ProductoViewModel()),
      ChangeNotifierProvider(create: (_) => PedidoViewModel()),
      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: 'Yoshi Papas',
      theme: AppTheme.lightTheme,
      initialRoute: 'main',
      routes: {
        'main': (context) => const MainScreen(),
        'home': (context) => const HomeScreen(),
      },
    );
  }
}
