/// Project Progress Local Data Source
///
/// Handles local caching of project progress list using Hive.
library;

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

/// Project Progress Local Data Source
class ProjectProgressLocalDataSource {
  /// Get Hive box for project progress
  Box<dynamic> get _box => Hive.box(HiveBoxNames.projectProgressBox);

  /// Save project progress list to cache
  Future<void> cacheProgressList(List<ProjectProgressModel> progressList) async {
    // Clear existing cache
    await _box.clear();

    // Save each progress with its id as key
    for (final progress in progressList) {
      await _box.put(progress.id, progress);
    }
  }

  /// Get cached project progress list
  List<ProjectProgressModel> getCachedProgressList() {
    try {
      final values = _box.values.whereType<ProjectProgressModel>().toList();
      return values;
    } catch (e) {
      return [];
    }
  }

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

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