import 'package:hive_flutter/hive_flutter.dart';
import 'package:barber_app/features/auth/data/models/user_model.dart';
import 'package:barber_app/features/haircuts/data/models/haircut_model.dart';
import 'package:barber_app/features/products/data/models/product_model.dart';
import 'package:barber_app/features/finances/data/models/transaction_model.dart';
import 'package:barber_app/features/services/data/models/service_model.dart';
import 'package:barber_app/features/settings/data/models/settings_model.dart';

class DatabaseService {
  static const String usersBox = 'users';
  static const String haircutsBox = 'haircuts';
  static const String productsBox = 'products';
  static const String transactionsBox = 'transactions';
  static const String settingsBox = 'settings';
  static const String servicesBox = 'services';
  static const String currentUserBox = 'currentUser';
  
  static bool _initialized = false;
  
  static Future<void> init() async {
    if (_initialized) return;
    
    await Hive.initFlutter();
    
    // Registra os adapters apenas se não foram registrados
    if (!Hive.isAdapterRegistered(0)) {
      Hive.registerAdapter(UserModelAdapter());
    }
    if (!Hive.isAdapterRegistered(1)) {
      Hive.registerAdapter(HaircutModelAdapter());
    }
    if (!Hive.isAdapterRegistered(2)) {
      Hive.registerAdapter(ProductModelAdapter());
    }
    if (!Hive.isAdapterRegistered(3)) {
      Hive.registerAdapter(TransactionTypeAdapter());
    }
    if (!Hive.isAdapterRegistered(4)) {
      Hive.registerAdapter(TransactionModelAdapter());
    }
    if (!Hive.isAdapterRegistered(5)) {
      Hive.registerAdapter(SettingsModelAdapter());
    }
    if (!Hive.isAdapterRegistered(6)) {
      Hive.registerAdapter(ServiceModelAdapter());
    }
    
    // Abre as boxes
    await Hive.openBox<UserModel>(usersBox);
    await Hive.openBox<HaircutModel>(haircutsBox);
    await Hive.openBox<ProductModel>(productsBox);
    await Hive.openBox<TransactionModel>(transactionsBox);
    await Hive.openBox<SettingsModel>(settingsBox);
    await Hive.openBox<ServiceModel>(servicesBox);
    await Hive.openBox(currentUserBox);
    
    _initialized = true;
  }
  
  // User Box
  static Box<UserModel> get usersBoxInstance => Hive.box<UserModel>(usersBox);
  
  // Haircuts Box
  static Box<HaircutModel> get haircutsBoxInstance => Hive.box<HaircutModel>(haircutsBox);
  
  // Products Box
  static Box<ProductModel> get productsBoxInstance => Hive.box<ProductModel>(productsBox);
  
  // Transactions Box
  static Box<TransactionModel> get transactionsBoxInstance => Hive.box<TransactionModel>(transactionsBox);
  
  // Settings Box
  static Box<SettingsModel> get settingsBoxInstance => Hive.box<SettingsModel>(settingsBox);

  // Services Box
  static Box<ServiceModel> get servicesBoxInstance => Hive.box<ServiceModel>(servicesBox);
  
  // Current User
  static Box get currentUserBoxInstance => Hive.box(currentUserBox);
  
  // Métodos de sessão
  static Future<void> setCurrentUserId(String? userId) async {
    if (userId == null) {
      await currentUserBoxInstance.delete('currentUserId');
    } else {
      await currentUserBoxInstance.put('currentUserId', userId);
    }
  }
  
  static String? getCurrentUserId() {
    return currentUserBoxInstance.get('currentUserId');
  }
  
  static bool isLoggedIn() {
    return getCurrentUserId() != null;
  }
}
