import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:turquessa_mesas_hoster/mvvm/viewmodels/creacion_pedido_view_model.dart';
import 'package:turquessa_mesas_hoster/mvvm/views/pedido/creacion_pedido_screen.dart';
import 'dart:io';

import 'core/models/models.dart';
import 'core/services/services.dart';
import 'mvvm/views/home/home_screen.dart';
import 'mvvm/views/login/login_screen.dart';
import '/utils/themes.dart';
import 'mvvm/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;
  }

  Database? db = await DatabaseService().db;

  final repositoryFactory = SQLiteRepositoryFactory(db!);

  // 3) Obtener el repositorio de Mesa
  final mesaRepository = repositoryFactory.getRepository<Mesa>(
    tableName: 'Mesa',
    fromMap: Mesa.fromMap,
    toMap: (m) => m.toMap(),
  );

  final categoriaRepository =
      repositoryFactory.getRepository<CategoriaProducto>(
    tableName: 'CategoriaProducto',
    fromMap: CategoriaProducto.fromMap,
    toMap: (m) => m.toMap(),
  );

  final productoRepository = repositoryFactory.getRepository<Producto>(
    tableName: 'Producto',
    fromMap: Producto.fromMap,
    toMap: (m) => m.toMap(),
  );

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeRight,
    DeviceOrientation.landscapeLeft,
  ]).then((_) {
    runApp(MultiProvider(providers: [
      ChangeNotifierProvider(create: (_) => LoginViewModel()),
      ChangeNotifierProvider(create: (_) => SucursalViewModel()),
      ChangeNotifierProvider(create: (_) => PermisoViewModel()),
      ChangeNotifierProvider(create: (_) => UsuarioViewModel()),
      ChangeNotifierProvider(create: (_) => ProductoViewModel()),
      ChangeNotifierProvider(
          create: (_) => HomeViewModel(
              categoriaRepository: categoriaRepository,
              productoRepository: productoRepository)),
      ChangeNotifierProvider(
          create: (_) => MesaViewModel(mesaRepository: mesaRepository)),
      ChangeNotifierProvider(create: (_) => CategoriaProductoViewModel()),
      ChangeNotifierProvider(create: (_) => CreacionPedidoViewModel())
      // 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: 'Turquessa',
      theme: AppTheme.lightTheme,
      initialRoute: 'login',
      routes: {
        'login': (context) => const LoginScreen(),
        'home': (context) => const HomeScreen(),
        'creacion-pedido': (context) => const CreacionPedido(),
      },
    );
  }
}
