import 'package:localstore/localstore.dart';

import '../api/request_cache.dart';

class DataCleaner {
  static Future<void> cleanOldCache() async {
    final cacheData = await Localstore.instance
        .collection(RequestCache.collection)
        .get();
    cacheData?.forEach((key, value) async {
      final lastUpdate = DateTime.fromMillisecondsSinceEpoch(
        ((value['lastupdate'] as num?) ?? 0).toInt(),
      );
      if (DateTime.now()
          .subtract(const Duration(days: 200))
          .isAfter(lastUpdate)) {
        await Localstore.instance
            .collection(RequestCache.collection)
            .doc(key.split('/').last)
            .delete();
      }
    });
  }
}
