import '../../about/external/data/ExcuseCluster.dart';
import '../../about/external/initial/ReportCardRequest.dart';
import '../../utils/ObjectConvertors.dart';
import '../parent/ParentViewModel.dart';
import 'ConnectExcuseReport.dart';

class ViewExcuseReport extends ParentViewModel {
  ConnectExcuseReport connection;

  ViewExcuseReport(super.context, this.connection);

  void loadClusters() async {
    if (!await hasNetwork(() => loadClusters())) return;

    showLoading("Reading your excuses");

    try {
      final DateTime now = DateTime.now();
      final DateTime start = now.subtract(const Duration(days: 30));

      final response =
          await getDataManager().getExcuseClusters(ReportCardRequest(
        periodStart: start.toIso8601String(),
        periodEnd: now.toIso8601String(),
      ));

      final List<ExcuseCluster> clusters =
          getExcuseClusterList(response.data);

      closeLoading();

      connection.onClustersLoaded(clusters);
    } catch (e) {
      handleError(e, () => loadClusters(), () => dismissError(), "Retry");
    }
  }
}
