import '../../about/external/data/ReportCard.dart';
import '../../about/external/initial/ReportCardRequest.dart';
import '../parent/ParentViewModel.dart';
import 'ConnectReportCardScreen.dart';

class ViewReportCardScreen extends ParentViewModel {
  ConnectReportCardScreen connection;

  ViewReportCardScreen(super.context, this.connection);

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

    showLoading("Preparing your report");

    try {
      final DateTime now = DateTime.now();

      // The week runs Monday to now, so the card always covers the week you
      // are actually in rather than a trailing seven days.
      final DateTime start =
          now.subtract(Duration(days: now.weekday - 1));

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

      closeLoading();

      connection.onReportLoaded(ReportCard.fromJson(response.data));
    } catch (e) {
      handleError(e, () => loadReport(), () => dismissError(), "Retry");
    }
  }
}
