getSymptomsPatternForReport method

Future<List<SymptomsPatterns>> getSymptomsPatternForReport()

Implementation

Future<List<SymptomsPatterns>> getSymptomsPatternForReport() async {
  List<SymptomsPatterns> symptomAnalysisData = [];
  List<SymptomsPattern> symptomsPatternData =
      await getSymptomsPatternBasedOnCycle(numberOfCycle: 15);

  // printMenstrualCycleLogs("symptomsPatternData ${symptomsPatternData.length}");
  List<PeriodsDateRange> allPeriodRange = await getAllPeriodsDetails();
  int numberOfCycle = allPeriodRange.length;

  for (var symptomsPattern in symptomsPatternData) {
    SymptomsPatterns symptomsPatterns = SymptomsPatterns();

    int cyclesWithSymptom = symptomsPattern.cycleData!.where((cycleData) {
      return cycleData.cycleDates!
          .any((date) => date.isFoundSymptoms == true);
    }).length;

    double percentCyclesWithSymptom =
        (cyclesWithSymptom / numberOfCycle) * 100;

    int totalSymptomDays = 0;

    // Prepare map to count symptoms per phase
    Map<String, int> phaseSymptomCount = {
      WidgetBaseLanguage.menstruationLabel: 0,
      WidgetBaseLanguage.follicularPhaseLabel: 0,
      WidgetBaseLanguage.ovulationLabel: 0,
      WidgetBaseLanguage.lutealPhaseLabel: 0
    };

    // Process cycle data
    for (var cycleData in symptomsPattern.cycleData!) {
      for (int i = 0; i < cycleData.cycleDates!.length; i++) {
        var date = cycleData.cycleDates![i];
        if (date.isFoundSymptoms == true) {
          // Determine phase based on cycleDay if available, fallback to index
          int? cycleDayInt;
          if (date.cycleDay != null && date.cycleDay!.isNotEmpty) {
            cycleDayInt = int.tryParse(date.cycleDay!);
          }

          int cycleDay = cycleDayInt ?? (i + 1); // Fallback if null
          String phaseName;
          if (cycleDay >= 1 && cycleDay <= 5) {
            phaseName = WidgetBaseLanguage.menstruationLabel;
          } else if (cycleDay >= 6 && cycleDay <= 13) {
            phaseName = WidgetBaseLanguage.follicularPhaseLabel;
          } else if (cycleDay == 14) {
            phaseName = WidgetBaseLanguage.ovulationLabel;
          } else {
            phaseName = WidgetBaseLanguage.lutealPhaseLabel;
          }

          phaseSymptomCount[phaseName] =
              (phaseSymptomCount[phaseName] ?? 0) + 1;
          totalSymptomDays++;
        }
      }
    }

    // Prepare list of phases where symptom was found
    List<PhasePercentage> phasePercentage = [];

    phaseSymptomCount.forEach((phase, count) {
      double percent =
          totalSymptomDays > 0 ? (count / totalSymptomDays) * 100 : 0.0;
      phasePercentage.add(PhasePercentage(
        phaseName: phase,
        percentage: double.parse(percent.toStringAsFixed(1)),
      ));
    });
    // Set result
    symptomsPatterns.name = symptomsPattern.symptomsName;
    symptomsPatterns.percentageOfCycles = percentCyclesWithSymptom.round();
    symptomsPatterns.phases = phasePercentage;

    symptomAnalysisData.add(symptomsPatterns);
  }

  return symptomAnalysisData;
}