import 'package:hive_ce_flutter/hive_flutter.dart';
import 'package:worker/core/constants/storage_constants.dart';
import 'package:worker/features/products/data/models/product_model.dart';

/// Favorite Products Local DataSource
///
/// Caches the actual product data from wishlist API.
/// This is separate from ProductsLocalDataSource to avoid conflicts.
class FavoriteProductsLocalDataSource {
  /// Get the Hive box for favorite products
  Box<dynamic> get _box {
    return Hive.box<dynamic>(HiveBoxNames.favoriteProductsBox);
  }

  /// Get all favorite products from cache
  Future<List<ProductModel>> getAllProducts() async {
    try {
      final products = _box.values
          .whereType<ProductModel>()
          .toList();

      _debugPrint('Loaded ${products.length} favorite products from cache');
      return products;
    } catch (e) {
      _debugPrint('Error getting favorite products: $e');
      return [];
    }
  }

  /// Save products from wishlist API to cache
  Future<void> saveProducts(List<ProductModel> products) async {
    try {
      // Clear existing cache
      await _box.clear();

      // Save new products
      for (final product in products) {
        await _box.put(product.productId, product);
      }

      _debugPrint('Cached ${products.length} favorite products');
    } catch (e) {
      _debugPrint('Error saving favorite products: $e');
      rethrow;
    }
  }

  /// Clear all cached favorite products
  Future<void> clearAll() async {
    try {
      await _box.clear();
      _debugPrint('Cleared all favorite products cache');
    } catch (e) {
      _debugPrint('Error clearing favorite products: $e');
      rethrow;
    }
  }

  /// Check if the box is open
  bool isBoxOpen() {
    return Hive.isBoxOpen(HiveBoxNames.favoriteProductsBox);
  }

  /// Check if a product is in favorites (local only - no API call)
  bool isFavorite(String productId) {
    try {
      return _box.containsKey(productId);
    } catch (e) {
      _debugPrint('Error checking favorite: $e');
      return false;
    }
  }

  /// Get all favorite product IDs (local only - no API call)
  Set<String> getFavoriteIds() {
    try {
      return _box.keys.cast<String>().toSet();
    } catch (e) {
      _debugPrint('Error getting favorite IDs: $e');
      return {};
    }
  }

  /// Add a product to local favorites cache
  Future<void> addFavorite(ProductModel product) async {
    try {
      await _box.put(product.productId, product);
      _debugPrint('Added to local favorites: ${product.productId}');
    } catch (e) {
      _debugPrint('Error adding to local favorites: $e');
      rethrow;
    }
  }

  /// Remove a product from local favorites cache
  Future<void> removeFavorite(String productId) async {
    try {
      await _box.delete(productId);
      _debugPrint('Removed from local favorites: $productId');
    } catch (e) {
      _debugPrint('Error removing from local favorites: $e');
      rethrow;
    }
  }
}

/// Debug print helper
void _debugPrint(String message) {
  // ignore: avoid_print
  print('[FavoriteProductsLocalDataSource] $message');
}
