/// Order Status Local Data Source
///
/// Handles local caching of order status list using Hive.
library;

import 'package:hive_ce/hive.dart';
import 'package:worker/core/constants/storage_constants.dart';
import 'package:worker/features/orders/data/models/order_status_model.dart';

/// Order Status Local Data Source
class OrderStatusLocalDataSource {
  /// Get Hive box for order statuses
  Box<dynamic> get _box => Hive.box(HiveBoxNames.orderStatusBox);

  /// Save order status list to cache
  Future<void> cacheStatusList(List<OrderStatusModel> statuses) async {
    // Clear existing cache
    await _box.clear();

    // Save each status with its index as key
    for (final status in statuses) {
      await _box.put(status.index, status);
    }
  }

  /// Get cached order status list
  List<OrderStatusModel> getCachedStatusList() {
    try {
      final values = _box.values.whereType<OrderStatusModel>().toList()
      // Sort by index
      ..sort((a, b) => a.index.compareTo(b.index));
      return values;
    } catch (e) {
      return [];
    }
  }

  /// Check if cache exists and is not empty
  bool hasCachedData() {
    return _box.isNotEmpty;
  }

  /// Clear all cached statuses
  Future<void> clearCache() async {
    await _box.clear();
  }
}
