// Data Models for Workout Logger App import 'dart:math' show log, max; import 'dart:convert'; import 'package:uuid/uuid.dart'; import 'web_search_result.dart'; // Sentinel value for copyWith methods to distinguish "not provided" from "null" const Object _sentinel = Object(); const _uuid = Uuid(); // ==================== Muscle Groups ==================== class MuscleGroup { final String id; final String name; double growthRate; // Learned coefficient (volume increase per session) DateTime lastUpdated; MuscleGroup({ required this.id, required this.name, this.growthRate = 0.0, DateTime? lastUpdated, }) : lastUpdated = lastUpdated ?? DateTime.now(); Map toJson() => { 'id': id, 'name': name, 'growthRate': growthRate, 'lastUpdated': lastUpdated.toIso8601String(), }; factory MuscleGroup.fromJson(Map json) => MuscleGroup( id: json['id'], name: json['name'], growthRate: (json['growthRate'] as num?)?.toDouble() ?? 0.0, lastUpdated: DateTime.parse(json['lastUpdated']), ); } // ==================== Muscle Activation ==================== class MuscleActivation { final String muscleGroupId; final int activationPercentage; // 0-100 MuscleActivation({ required this.muscleGroupId, required this.activationPercentage, }); Map toJson() => { 'muscleGroupId': muscleGroupId, 'activationPercentage': activationPercentage, }; factory MuscleActivation.fromJson(Map json) => MuscleActivation( muscleGroupId: json['muscleGroupId'], activationPercentage: json['activationPercentage'], ); } // ==================== Exercise ==================== // Exercise IDs treated as bodyweight-assisted (e.g. an assisted-dip/pull-up // machine). Computed once here so every consumer (the load panel, the input // row, set persistence) agrees on which exercises count as "assisted". const Set _assistedBodyweightExerciseIds = { 'pull_ups', 'chin_ups', 'dips', }; bool isAssistedBodyweightExercise(String? exerciseId) => exerciseId != null && _assistedBodyweightExerciseIds.contains(exerciseId); /// Bodyweight movement IDs; push-ups default to added load, never assistance. bool isBodyweightExercise(String? id) => id == 'push_ups' || isAssistedBodyweightExercise(id); enum WorkoutLoadMode { external, assisted, weighted, unknown } enum ExerciseType { weightAndReps, timeBased; String get displayName { switch (this) { case ExerciseType.weightAndReps: return 'Weight & Reps'; case ExerciseType.timeBased: return 'Time-Based'; } } } class Exercise { final String id; final String name; final List _legacyMuscleActivations; final Map? _resistanceIntensities; Map get resistanceIntensities => _resistanceIntensities ?? { for (final muscle in _legacyMuscleActivations) muscle.muscleGroupId: muscle.activationPercentage / 10, }; /// Compatibility for existing analytics; persistence uses intensity maps. List get muscleActivations => _resistanceIntensities == null ? _legacyMuscleActivations : [ for (final entry in _resistanceIntensities.entries) MuscleActivation( muscleGroupId: entry.key, activationPercentage: (entry.value * 10).round(), ), ]; final Map? stretchIntensities; final bool resistanceIntensityOverride; final String category; // 'compound' or 'isolation' final bool isCustom; // User-created exercise final List? availableHandles; // Attachment/handle options e.g. ['Rope', 'Bar'] final ExerciseType exerciseType; const Exercise({ required this.id, required this.name, List muscleActivations = const [], Map? resistanceIntensities, required this.category, this.stretchIntensities, this.resistanceIntensityOverride = false, this.isCustom = false, this.availableHandles, this.exerciseType = ExerciseType.weightAndReps, }) : _legacyMuscleActivations = muscleActivations, _resistanceIntensities = resistanceIntensities; Exercise copyWith({ String? id, String? name, List? muscleActivations, Map? resistanceIntensities, Map? stretchIntensities, bool? resistanceIntensityOverride, String? category, bool? isCustom, List? availableHandles, ExerciseType? exerciseType, }) => Exercise( id: id ?? this.id, name: name ?? this.name, muscleActivations: muscleActivations ?? const [], resistanceIntensities: resistanceIntensities ?? (muscleActivations == null ? this.resistanceIntensities : null), stretchIntensities: stretchIntensities ?? this.stretchIntensities, resistanceIntensityOverride: resistanceIntensityOverride ?? this.resistanceIntensityOverride, category: category ?? this.category, isCustom: isCustom ?? this.isCustom, availableHandles: availableHandles ?? this.availableHandles, exerciseType: exerciseType ?? this.exerciseType, ); String get primaryMuscle { if (muscleActivations.isEmpty) return 'Unknown'; final sorted = List.from( muscleActivations, )..sort((a, b) => b.activationPercentage.compareTo(a.activationPercentage)); return sorted.first.muscleGroupId; } Map toJson() => { 'id': id, 'name': name, 'resistanceIntensities': resistanceIntensities, if (stretchIntensities != null) 'stretchIntensities': stretchIntensities, if (resistanceIntensityOverride) 'resistanceIntensityOverride': true, 'category': category, 'isCustom': isCustom, 'availableHandles': availableHandles, 'exerciseType': exerciseType.name, }; factory Exercise.fromJson(Map json) { final rawType = json['exerciseType']; final ExerciseType type; if (rawType == null) { type = ExerciseType.weightAndReps; } else if (rawType == 'timeBased' || rawType == ExerciseType.timeBased.name) { type = ExerciseType.timeBased; } else if (rawType == 'weightAndReps' || rawType == ExerciseType.weightAndReps.name) { type = ExerciseType.weightAndReps; } else { throw ArgumentError('Unsupported exerciseType: $rawType'); } return Exercise( id: json['id'], name: json['name'], resistanceIntensities: decodeResistanceIntensities( json['resistanceIntensities'], ), muscleActivations: (json['muscleActivations'] as List? ?? []) .map((m) => MuscleActivation.fromJson(m)) .toList(), category: json['category'], isCustom: json['isCustom'] ?? false, availableHandles: (json['availableHandles'] as List?)?.cast(), exerciseType: type, stretchIntensities: decodeStretchIntensities(json['stretchIntensities']), resistanceIntensityOverride: json['resistanceIntensityOverride'] == true, ); } static Map? decodeStretchIntensities(dynamic input) { if (input == null) return null; if (input is! Map) { throw const FormatException('Invalid muscle intensity map.'); } final result = {}; for (final entry in input.entries) { if (entry.key is! String || entry.value is! int || entry.value < 1 || entry.value > 10) { throw const FormatException( 'Muscle intensity scores must be integers from 1 to 10.', ); } result[entry.key as String] = entry.value as int; } return Map.unmodifiable(result); } static Map? decodeResistanceIntensities(dynamic input) { if (input == null) return null; if (input is! Map) throw const FormatException('Invalid resistance map.'); final result = {}; for (final entry in input.entries) { if (entry.key is! String || entry.value is! num || !(entry.value as num).isFinite || entry.value < 0 || entry.value > 10) { throw const FormatException( 'Resistance scores must be between 0 and 10.', ); } result[entry.key as String] = entry.value as num; } return Map.unmodifiable(result); } } /// Formats a duration in seconds to a human-friendly string (e.g. "45s", "1m 30s", "2m"). String formatHoldDuration(int seconds) { if (seconds <= 0) return '0s'; if (seconds < 60) return '${seconds}s'; final m = seconds ~/ 60; final s = seconds % 60; return s == 0 ? '${m}m' : '${m}m ${s}s'; } // ==================== Workout Set ==================== class WorkoutSet { final double weight; // in kg final int reps; final bool isDropset; final List? drops; // For dropsets final int? timeTaken; // seconds final DateTime timestamp; final double? assistWeight; final double? extraWeight; final String? handle; final double? bodyWeightAtLog; final WorkoutLoadMode loadMode; final int loadEncodingVersion; WorkoutSet({ required this.weight, required this.reps, this.isDropset = false, this.drops, this.timeTaken, DateTime? timestamp, this.assistWeight, this.extraWeight, this.handle, this.bodyWeightAtLog, WorkoutLoadMode? loadMode, int? loadEncodingVersion, }) : loadMode = loadMode ?? (assistWeight == null ? WorkoutLoadMode.external : WorkoutLoadMode.assisted), loadEncodingVersion = loadEncodingVersion ?? (((loadMode ?? (assistWeight == null ? WorkoutLoadMode.external : WorkoutLoadMode.assisted)) != WorkoutLoadMode.external) && bodyWeightAtLog != null ? 1 : 0), timestamp = timestamp ?? DateTime.now(); /// Per-rep effective load for the main (non-drop) entry of this set: for /// assisted-bodyweight sets (i.e. [assistWeight] is set) this is /// `bodyweight − assist + extra`, snapshotted against [bodyWeightAtLog] /// (falling back to 70.0) so historical values stay correct even if the /// user's current bodyweight later changes. Conventional (non-assisted) /// sets just use [weight]. Use this (not raw [weight]) wherever a /// "how heavy was this set" comparison needs to be consistent with /// [calculateVolume] for assisted-bodyweight exercises. double get effectiveWeight => effectiveLoad( loadMode == WorkoutLoadMode.assisted ? (assistWeight ?? weight) : weight, ); /// Version 0 preserves raw historical data. Version 1 uses a recorded BW. /// Consumers must compare only matching conventions, never backfill BW. String get loadConvention => '$loadEncodingVersion:${loadMode == WorkoutLoadMode.unknown ? 'unknown' : loadMode == WorkoutLoadMode.external ? 'external' : 'bodyweight'}'; bool hasComparableLoad(WorkoutSet other) => loadConvention == other.loadConvention; double effectiveLoad(double enteredWeight, {double? userBodyWeight}) { if (loadMode == WorkoutLoadMode.unknown) return 0; if (loadMode == WorkoutLoadMode.external) return enteredWeight; final bw = bodyWeightAtLog ?? userBodyWeight ?? 70.0; if (loadMode == WorkoutLoadMode.weighted) { return max(0.0, bw + enteredWeight); } return max(0.0, bw - enteredWeight + (extraWeight ?? 0.0)); } /// Normalize main and drop loads together for analytics/recommendations. WorkoutSet toEffectiveLoad() => copyWith( weight: effectiveWeight, assistWeight: null, extraWeight: null, bodyWeightAtLog: null, loadMode: loadMode == WorkoutLoadMode.unknown ? WorkoutLoadMode.unknown : WorkoutLoadMode.external, loadEncodingVersion: 0, drops: drops ?.map( (drop) => DropsetEntry( id: drop.id, weight: effectiveLoad(drop.weight), reps: drop.reps, ), ) .toList(), ); double calculateVolume({double? userBodyWeight, bool? isAssistedBW}) { if (loadMode == WorkoutLoadMode.unknown) return 0; // Explicit override is retained only for legacy callers. Stored modes win. double load(double value) => loadMode == WorkoutLoadMode.external && isAssistedBW == true ? max( 0.0, (bodyWeightAtLog ?? userBodyWeight ?? 70.0) - value + (extraWeight ?? 0.0), ) : effectiveLoad(value, userBodyWeight: userBodyWeight); final effW = load( loadMode == WorkoutLoadMode.assisted ? (assistWeight ?? weight) : weight, ); double vol = reps == 0 && (timeTaken != null && timeTaken! > 0) ? (effW > 0 ? effW : 1.0) * timeTaken! : effW * reps; if (isDropset && drops != null) { for (final drop in drops!) { vol += load(drop.weight) * drop.reps; } } return vol; } double get volume => calculateVolume(); /// True when this set represents an isometric time-based hold rather than rep-based work. bool get isTimeBased => reps == 0 && (timeTaken != null && timeTaken! > 0); /// Human-friendly duration string (e.g. "45s", "1m 30s") String get formattedDuration => formatHoldDuration(timeTaken ?? 0); Map toJson() => { 'weight': weight, 'reps': reps, 'isDropset': isDropset, 'drops': drops?.map((d) => d.toJson()).toList(), 'timeTaken': timeTaken, 'timestamp': timestamp.toIso8601String(), 'assistWeight': assistWeight, 'extraWeight': extraWeight, 'handle': handle, 'bodyWeightAtLog': bodyWeightAtLog, 'loadMode': loadMode.name, 'loadEncodingVersion': loadEncodingVersion, }; factory WorkoutSet.fromJson(Map json) => WorkoutSet( weight: (json['weight'] as num).toDouble(), reps: json['reps'], isDropset: json['isDropset'] ?? false, drops: json['drops'] != null ? (json['drops'] as List).map((d) => DropsetEntry.fromJson(d)).toList() : null, timeTaken: json['timeTaken'], timestamp: DateTime.parse(json['timestamp']), assistWeight: (json['assistWeight'] as num?)?.toDouble(), extraWeight: (json['extraWeight'] as num?)?.toDouble(), handle: json['handle'] as String?, bodyWeightAtLog: (json['bodyWeightAtLog'] as num?)?.toDouble(), loadMode: json['loadMode'] == null ? null : WorkoutLoadMode.values.byName(json['loadMode'] as String), loadEncodingVersion: json['loadEncodingVersion'] as int?, ); WorkoutSet copyWith({ Object? weight = _sentinel, Object? reps = _sentinel, Object? isDropset = _sentinel, Object? drops = _sentinel, Object? timeTaken = _sentinel, Object? timestamp = _sentinel, Object? assistWeight = _sentinel, Object? extraWeight = _sentinel, Object? handle = _sentinel, Object? bodyWeightAtLog = _sentinel, WorkoutLoadMode? loadMode, int? loadEncodingVersion, }) => WorkoutSet( weight: weight == _sentinel ? this.weight : weight as double, reps: reps == _sentinel ? this.reps : reps as int, isDropset: isDropset == _sentinel ? this.isDropset : isDropset as bool, drops: drops == _sentinel ? this.drops : drops as List?, timeTaken: timeTaken == _sentinel ? this.timeTaken : timeTaken as int?, timestamp: timestamp == _sentinel ? this.timestamp : timestamp as DateTime?, assistWeight: assistWeight == _sentinel ? this.assistWeight : assistWeight as double?, extraWeight: extraWeight == _sentinel ? this.extraWeight : extraWeight as double?, handle: handle == _sentinel ? this.handle : handle as String?, bodyWeightAtLog: bodyWeightAtLog == _sentinel ? this.bodyWeightAtLog : bodyWeightAtLog as double?, loadMode: loadMode ?? this.loadMode, loadEncodingVersion: loadEncodingVersion ?? this.loadEncodingVersion, ); } class DropsetEntry { final String id; final double weight; final int reps; DropsetEntry({String? id, required this.weight, required this.reps}) : id = id ?? _uuid.v4(); Map toJson() => {'id': id, 'weight': weight, 'reps': reps}; factory DropsetEntry.fromJson(Map json) => DropsetEntry( id: json['id'] as String?, weight: (json['weight'] as num).toDouble(), reps: json['reps'], ); } // ==================== Exercise Log ==================== class ExerciseLog { final String exerciseId; final List sets; final String? notes; final String? handle; const ExerciseLog({ required this.exerciseId, required this.sets, this.notes, this.handle, }); double calculateTotalVolume({double? userBodyWeight, bool? isAssistedBW}) => sets.fold( 0.0, (sum, set) => sum + set.calculateVolume( userBodyWeight: userBodyWeight, isAssistedBW: isAssistedBW, ), ); double get totalVolume => sets.fold(0.0, (sum, set) => sum + set.volume); /// Total hold duration in seconds across all sets for time-based exercises. int get totalHoldDuration => sets.fold(0, (sum, set) => sum + (set.timeTaken ?? 0)); /// True when all logged sets represent time-based holds. bool get isTimeBased => sets.isNotEmpty && sets.every((s) => s.isTimeBased); Map toJson() => { 'exerciseId': exerciseId, 'sets': sets.map((s) => s.toJson()).toList(), 'notes': notes, 'handle': handle, }; factory ExerciseLog.fromJson(Map json) => ExerciseLog( exerciseId: json['exerciseId'], sets: (json['sets'] as List).map((s) => WorkoutSet.fromJson(s)).toList(), notes: json['notes'], handle: json['handle'] as String?, ); ExerciseLog copyWith({ Object? exerciseId = _sentinel, Object? sets = _sentinel, Object? notes = _sentinel, Object? handle = _sentinel, }) => ExerciseLog( exerciseId: exerciseId == _sentinel ? this.exerciseId : exerciseId as String, sets: sets == _sentinel ? this.sets : sets as List, notes: notes == _sentinel ? this.notes : notes as String?, handle: handle == _sentinel ? this.handle : handle as String?, ); } // ==================== Workout Session ==================== class WorkoutSession { final String id; final DateTime date; final String? routineId; final String? programId; final String? programDayId; final int? programWeekNumber; final List exercises; final int duration; // minutes final String? notes; /// Source identity, original payload and last imported content snapshot. final Map? importSource; /// Non-null when this session was successfully synced to Health Connect. final DateTime? hcSyncedAt; /// Optional once-per-workout subjective effort (1 = Easy, 2 = Solid, /// 3 = Brutal), captured on the post-workout summary screen. Used only to /// calibrate [EffortEstimator]'s per-set RPE anchor — never required. final int? sessionEffort; WorkoutSession({ required this.id, required this.date, this.routineId, this.programId, this.programDayId, this.programWeekNumber, required this.exercises, required this.duration, this.notes, this.importSource, this.hcSyncedAt, this.sessionEffort, }); double get totalVolume => exercises.fold(0.0, (sum, ex) => sum + ex.totalVolume); Map toJson() => { 'id': id, 'date': date.toIso8601String(), 'routineId': routineId, 'programId': programId, 'programDayId': programDayId, 'programWeekNumber': programWeekNumber, 'exercises': exercises.map((e) => e.toJson()).toList(), 'duration': duration, 'notes': notes, if (importSource != null) 'importSource': importSource, 'hcSyncedAt': hcSyncedAt?.toIso8601String(), 'sessionEffort': sessionEffort, }; factory WorkoutSession.fromJson(Map json) => WorkoutSession( id: json['id'], date: DateTime.parse(json['date']), routineId: json['routineId'], programId: json['programId'] as String?, programDayId: json['programDayId'] as String?, programWeekNumber: json['programWeekNumber'] as int?, exercises: (json['exercises'] as List) .map((e) => ExerciseLog.fromJson(e)) .toList(), duration: json['duration'], notes: json['notes'], importSource: json['importSource'] == null ? null : Map.from(json['importSource'] as Map), hcSyncedAt: json['hcSyncedAt'] != null ? DateTime.parse(json['hcSyncedAt'] as String) : null, sessionEffort: json['sessionEffort'] as int?, ); WorkoutSession copyWith({ Object? id = _sentinel, Object? date = _sentinel, Object? routineId = _sentinel, Object? programId = _sentinel, Object? programDayId = _sentinel, Object? programWeekNumber = _sentinel, Object? exercises = _sentinel, Object? duration = _sentinel, Object? notes = _sentinel, Object? hcSyncedAt = _sentinel, Object? importSource = _sentinel, Object? sessionEffort = _sentinel, }) => WorkoutSession( id: id == _sentinel ? this.id : id as String, date: date == _sentinel ? this.date : date as DateTime, routineId: routineId == _sentinel ? this.routineId : routineId as String?, programId: programId == _sentinel ? this.programId : programId as String?, programDayId: programDayId == _sentinel ? this.programDayId : programDayId as String?, programWeekNumber: programWeekNumber == _sentinel ? this.programWeekNumber : programWeekNumber as int?, exercises: exercises == _sentinel ? this.exercises : exercises as List, duration: duration == _sentinel ? this.duration : duration as int, notes: notes == _sentinel ? this.notes : notes as String?, hcSyncedAt: hcSyncedAt == _sentinel ? this.hcSyncedAt : hcSyncedAt as DateTime?, importSource: importSource == _sentinel ? this.importSource : importSource as Map?, sessionEffort: sessionEffort == _sentinel ? this.sessionEffort : sessionEffort as int?, ); } // ==================== Routine ==================== const _routineSentinel = Object(); class Routine { final String id; final String name; final List exerciseIds; final DateTime createdAt; final Map? defaultHandles; Routine({ required this.id, required this.name, required this.exerciseIds, DateTime? createdAt, this.defaultHandles, }) : createdAt = createdAt ?? DateTime.now(); Map toJson() => { 'id': id, 'name': name, 'exerciseIds': exerciseIds, 'createdAt': createdAt.toIso8601String(), 'defaultHandles': defaultHandles, }; factory Routine.fromJson(Map json) => Routine( id: json['id'], name: json['name'], exerciseIds: List.from(json['exerciseIds']), createdAt: DateTime.parse(json['createdAt']), defaultHandles: (json['defaultHandles'] as Map?)?.cast(), ); Routine copyWith({ String? name, List? exerciseIds, Object? defaultHandles = _routineSentinel, }) => Routine( id: id, name: name ?? this.name, exerciseIds: exerciseIds ?? this.exerciseIds, createdAt: createdAt, defaultHandles: identical(defaultHandles, _routineSentinel) ? this.defaultHandles : (defaultHandles is Map ? defaultHandles.cast() : null), ); } // ==================== Target ==================== class Target { final String id; final String exerciseId; final String targetType; // 'reps', 'weight', 'volume' final double targetValue; double currentValue; DateTime? estimatedCompletionDate; final DateTime createdAt; bool isCompleted; Target({ required this.id, required this.exerciseId, required this.targetType, required this.targetValue, this.currentValue = 0.0, this.estimatedCompletionDate, DateTime? createdAt, this.isCompleted = false, }) : createdAt = createdAt ?? DateTime.now(); double get progressPercentage => (currentValue / targetValue * 100).clamp(0, 100); Map toJson() => { 'id': id, 'exerciseId': exerciseId, 'targetType': targetType, 'targetValue': targetValue, 'currentValue': currentValue, 'estimatedCompletionDate': estimatedCompletionDate?.toIso8601String(), 'createdAt': createdAt.toIso8601String(), 'isCompleted': isCompleted, }; factory Target.fromJson(Map json) => Target( id: json['id'], exerciseId: json['exerciseId'], targetType: json['targetType'], targetValue: (json['targetValue'] as num).toDouble(), currentValue: (json['currentValue'] as num?)?.toDouble() ?? 0.0, estimatedCompletionDate: json['estimatedCompletionDate'] != null ? DateTime.parse(json['estimatedCompletionDate']) : null, createdAt: DateTime.parse(json['createdAt']), isCompleted: json['isCompleted'] ?? false, ); } // ==================== Set Recommendation ==================== class SetRecommendation { final double weight; final int reps; final int? targetDuration; // Target hold duration in seconds for time-based exercises final String confidence; // 'high', 'medium', 'low' final String reasoning; SetRecommendation({ required this.weight, required this.reps, this.targetDuration, required this.confidence, required this.reasoning, }); } // ==================== Effort Estimate ==================== /// Where an [EffortEstimate]'s RPE value came from. /// /// No per-set RPE picker exists in this app — [userReported] is reserved /// for if one is ever added, so estimated values can never be confused with /// a real logged one. enum EffortSource { userReported, estimatedWithHr, estimatedHrless } /// A 0–10 RPE (rate of perceived exertion) estimate for one set, inferred /// from data already logged rather than asked of the user. See /// [EffortEstimator] for how it's computed. class EffortEstimate { final double rpe; final EffortSource source; /// 0–1. Capped below 1.0 (see [EffortEstimator.maxEstimatedConfidence]) — /// an estimate can never claim the certainty a real user-reported value /// would have. final double confidence; const EffortEstimate({ required this.rpe, required this.source, required this.confidence, }); } // ==================== Growth Model ==================== /// Functional form of a fitted growth curve. /// /// - [linear]: steady volume gains (typical for newer lifters / new exercises) /// - [logarithmic]: diminishing returns, y = a + b·ln(1+x) — typical as an /// exercise matures and progress saturates enum GrowthCurve { linear, logarithmic } class GrowthModel { /// Instantaneous growth rate (volume per day) at the most recent data point. /// For linear fits this equals the curve coefficient; for logarithmic fits /// it is the tangent slope b/(1+lastX), which decays as training history grows. final double slope; final double intercept; // Curve intercept a final double r2; // Model fit quality (0-1) final DateTime lastTrained; final GrowthCurve curve; /// Curve coefficient b. Equals [slope] for linear fits. final double coefficient; /// x (days since first session) of the newest point used in training. final double lastX; /// Weighted residual standard error in volume units (0 = unknown/perfect). final double stdError; GrowthModel({ required this.slope, required this.intercept, required this.r2, required this.lastTrained, this.curve = GrowthCurve.linear, double? coefficient, this.lastX = 0, this.stdError = 0, }) : coefficient = coefficient ?? slope; double predict(num x) { switch (curve) { case GrowthCurve.linear: return intercept + coefficient * x; case GrowthCurve.logarithmic: return intercept + coefficient * log(1 + max(0, x.toDouble())); } } /// Model's volume estimate at the newest training point ("today's level"). double get currentEstimate => predict(lastX); /// Expected volume growth over the next 7 days as a percentage of the /// current level. The plateau/decline signal used by recommendations. double get weeklyGrowthPercent { final current = currentEstimate; if (current <= 0) return 0; return slope * 7 / current * 100; } } // ==================== Personal Record ==================== class PersonalRecord { final String exerciseId; final String? handle; final int loadEncodingVersion; final double bestWeight; final int bestReps; final double bestVolume; final DateTime achievedAt; final int? bestDuration; PersonalRecord({ required String exerciseId, String? handle, this.loadEncodingVersion = 0, required this.bestWeight, required this.bestReps, required this.bestVolume, required this.achievedAt, this.bestDuration, }) : exerciseId = handle == null && exerciseId.contains(':') ? exerciseId.substring(0, exerciseId.indexOf(':')) : exerciseId, handle = handle == null && exerciseId.contains(':') ? exerciseId.substring(exerciseId.indexOf(':') + 1) : (handle?.isEmpty == true ? null : handle); String get storageKey => jsonEncode([exerciseId, handle ?? '', loadEncodingVersion]); Map toJson() => { 'exerciseId': exerciseId, 'handle': handle, 'loadEncodingVersion': loadEncodingVersion, 'bestWeight': bestWeight, 'bestReps': bestReps, 'bestVolume': bestVolume, 'achievedAt': achievedAt.toIso8601String(), 'bestDuration': bestDuration, }; factory PersonalRecord.fromJson(Map json) => PersonalRecord( exerciseId: json['exerciseId'] as String, handle: json['handle'] as String?, loadEncodingVersion: json['loadEncodingVersion'] as int? ?? 0, bestWeight: (json['bestWeight'] as num).toDouble(), bestReps: json['bestReps'] as int, bestVolume: (json['bestVolume'] as num).toDouble(), achievedAt: DateTime.parse(json['achievedAt'] as String), bestDuration: json['bestDuration'] as int?, ); PersonalRecord copyWith({ double? bestWeight, int? bestReps, double? bestVolume, DateTime? achievedAt, int? bestDuration, }) => PersonalRecord( exerciseId: exerciseId, handle: handle, loadEncodingVersion: loadEncodingVersion, bestWeight: bestWeight ?? this.bestWeight, bestReps: bestReps ?? this.bestReps, bestVolume: bestVolume ?? this.bestVolume, achievedAt: achievedAt ?? this.achievedAt, bestDuration: bestDuration ?? this.bestDuration, ); /// Exercise-wide lookup combines only the latest measurement convention. static PersonalRecord? aggregate(Iterable records) { final all = records.toList(); if (all.isEmpty) return null; final version = all.map((r) => r.loadEncodingVersion).reduce(max); final compatible = all .where((r) => r.loadEncodingVersion == version) .toList(); return PersonalRecord( exerciseId: compatible.first.exerciseId, loadEncodingVersion: version, bestWeight: compatible.map((r) => r.bestWeight).reduce(max), bestReps: compatible.map((r) => r.bestReps).reduce(max), bestVolume: compatible.map((r) => r.bestVolume).reduce(max), bestDuration: compatible.map((r) => r.bestDuration ?? 0).reduce(max) == 0 ? null : compatible.map((r) => r.bestDuration ?? 0).reduce(max), achievedAt: compatible .map((r) => r.achievedAt) .reduce((a, b) => a.isAfter(b) ? a : b), ); } } // ==================== Training Program ==================== /// One exercise slot inside a program day. /// /// Holds all programming parameters: sets, rep range, rest, tempo, weight%, notes, /// and an optional superset group ID to visually link paired/grouped exercises. class ProgramExerciseSlot { final String exerciseId; final int sets; final int minReps; final int maxReps; final int restSeconds; final String? tempo; // e.g. "3-1-1" (eccentric-pause-concentric) final double? weightPercentage; // % of working weight / 1RM hint final String? notes; final String? supersetGroupId; // non-null → belongs to a superset ProgramExerciseSlot({ required this.exerciseId, required this.sets, required this.minReps, required this.maxReps, required this.restSeconds, this.tempo, this.weightPercentage, this.notes, this.supersetGroupId, }); Map toJson() => { 'exerciseId': exerciseId, 'sets': sets, 'minReps': minReps, 'maxReps': maxReps, 'restSeconds': restSeconds, 'tempo': tempo, 'weightPercentage': weightPercentage, 'notes': notes, 'supersetGroupId': supersetGroupId, }; factory ProgramExerciseSlot.fromJson(Map json) => ProgramExerciseSlot( exerciseId: json['exerciseId'] as String, sets: json['sets'] as int, minReps: json['minReps'] as int, maxReps: json['maxReps'] as int, restSeconds: json['restSeconds'] as int, tempo: json['tempo'] as String?, weightPercentage: (json['weightPercentage'] as num?)?.toDouble(), notes: json['notes'] as String?, supersetGroupId: json['supersetGroupId'] as String?, ); ProgramExerciseSlot copyWith({ Object? exerciseId = _sentinel, Object? sets = _sentinel, Object? minReps = _sentinel, Object? maxReps = _sentinel, Object? restSeconds = _sentinel, Object? tempo = _sentinel, Object? weightPercentage = _sentinel, Object? notes = _sentinel, Object? supersetGroupId = _sentinel, }) => ProgramExerciseSlot( exerciseId: exerciseId == _sentinel ? this.exerciseId : exerciseId as String, sets: sets == _sentinel ? this.sets : sets as int, minReps: minReps == _sentinel ? this.minReps : minReps as int, maxReps: maxReps == _sentinel ? this.maxReps : maxReps as int, restSeconds: restSeconds == _sentinel ? this.restSeconds : restSeconds as int, tempo: tempo == _sentinel ? this.tempo : tempo as String?, weightPercentage: weightPercentage == _sentinel ? this.weightPercentage : weightPercentage as double?, notes: notes == _sentinel ? this.notes : notes as String?, supersetGroupId: supersetGroupId == _sentinel ? this.supersetGroupId : supersetGroupId as String?, ); } /// One training day inside a program week. class ProgramDay { final String id; final String name; // e.g. "Push", "Pull", "Legs", "Core" final int? dayOfWeek; // 1=Mon … 7=Sun; null = unscheduled final String? notes; final List exercises; ProgramDay({ required this.id, required this.name, this.dayOfWeek, this.notes, required this.exercises, }); Map toJson() => { 'id': id, 'name': name, 'dayOfWeek': dayOfWeek, 'notes': notes, 'exercises': exercises.map((e) => e.toJson()).toList(), }; factory ProgramDay.fromJson(Map json) => ProgramDay( id: json['id'] as String, name: json['name'] as String, dayOfWeek: json['dayOfWeek'] as int?, notes: json['notes'] as String?, exercises: (json['exercises'] as List) .map((e) => ProgramExerciseSlot.fromJson(e as Map)) .toList(), ); ProgramDay copyWith({ Object? id = _sentinel, Object? name = _sentinel, Object? dayOfWeek = _sentinel, Object? notes = _sentinel, Object? exercises = _sentinel, }) => ProgramDay( id: id == _sentinel ? this.id : id as String, name: name == _sentinel ? this.name : name as String, dayOfWeek: dayOfWeek == _sentinel ? this.dayOfWeek : dayOfWeek as int?, notes: notes == _sentinel ? this.notes : notes as String?, exercises: exercises == _sentinel ? this.exercises : exercises as List, ); } /// One week of a training program. /// /// A deload week lowers volume/intensity to facilitate recovery. /// [deloadIntensityFactor] of 0.85 means 85% of normal weight. /// [deloadSetReduction] removes N sets per exercise (e.g. 1 or 2). class ProgramWeek { final int weekNumber; // 1-based final bool isDeload; final double deloadIntensityFactor; // 0.0–1.0; default 1.0 (no change) final int deloadSetReduction; // sets removed per exercise on deload final String? phaseId; final String? notes; final List days; ProgramWeek({ required this.weekNumber, this.isDeload = false, this.deloadIntensityFactor = 1.0, this.deloadSetReduction = 0, this.phaseId, this.notes, required this.days, }); Map toJson() => { 'weekNumber': weekNumber, 'isDeload': isDeload, 'deloadIntensityFactor': deloadIntensityFactor, 'deloadSetReduction': deloadSetReduction, 'phaseId': phaseId, 'notes': notes, 'days': days.map((d) => d.toJson()).toList(), }; factory ProgramWeek.fromJson(Map json) => ProgramWeek( weekNumber: json['weekNumber'] as int, isDeload: json['isDeload'] as bool? ?? false, deloadIntensityFactor: (json['deloadIntensityFactor'] as num?)?.toDouble() ?? 1.0, deloadSetReduction: json['deloadSetReduction'] as int? ?? 0, phaseId: json['phaseId'] as String?, notes: json['notes'] as String?, days: (json['days'] as List) .map((d) => ProgramDay.fromJson(d as Map)) .toList(), ); ProgramWeek copyWith({ Object? weekNumber = _sentinel, Object? isDeload = _sentinel, Object? deloadIntensityFactor = _sentinel, Object? deloadSetReduction = _sentinel, Object? phaseId = _sentinel, Object? notes = _sentinel, Object? days = _sentinel, }) => ProgramWeek( weekNumber: weekNumber == _sentinel ? this.weekNumber : weekNumber as int, isDeload: isDeload == _sentinel ? this.isDeload : isDeload as bool, deloadIntensityFactor: deloadIntensityFactor == _sentinel ? this.deloadIntensityFactor : deloadIntensityFactor as double, deloadSetReduction: deloadSetReduction == _sentinel ? this.deloadSetReduction : deloadSetReduction as int, phaseId: phaseId == _sentinel ? this.phaseId : phaseId as String?, notes: notes == _sentinel ? this.notes : notes as String?, days: days == _sentinel ? this.days : days as List, ); } /// Named training phase (e.g. "Foundation", "Intensify", "Peak"). class TrainingPhase { final String id; final String name; final int startWeek; // 1-based, inclusive final int endWeek; // 1-based, inclusive final String? notes; final String? colorHex; // optional override for UI TrainingPhase({ required this.id, required this.name, required this.startWeek, required this.endWeek, this.notes, this.colorHex, }); Map toJson() => { 'id': id, 'name': name, 'startWeek': startWeek, 'endWeek': endWeek, 'notes': notes, 'colorHex': colorHex, }; factory TrainingPhase.fromJson(Map json) => TrainingPhase( id: json['id'] as String, name: json['name'] as String, startWeek: json['startWeek'] as int, endWeek: json['endWeek'] as int, notes: json['notes'] as String?, colorHex: json['colorHex'] as String?, ); TrainingPhase copyWith({ Object? id = _sentinel, Object? name = _sentinel, Object? startWeek = _sentinel, Object? endWeek = _sentinel, Object? notes = _sentinel, Object? colorHex = _sentinel, }) => TrainingPhase( id: id == _sentinel ? this.id : id as String, name: name == _sentinel ? this.name : name as String, startWeek: startWeek == _sentinel ? this.startWeek : startWeek as int, endWeek: endWeek == _sentinel ? this.endWeek : endWeek as int, notes: notes == _sentinel ? this.notes : notes as String?, colorHex: colorHex == _sentinel ? this.colorHex : colorHex as String?, ); } /// Top-level training program (e.g. "12-Week Hypertrophy Block"). /// /// Contains an ordered list of [ProgramWeek]s and named [TrainingPhase]s. /// Can be created in-app or imported from a JSON file. class TrainingProgram { final String id; final String name; final String? description; final int totalWeeks; final List phases; final List weeks; final String? author; final bool isImported; final DateTime createdAt; TrainingProgram({ required this.id, required this.name, this.description, required this.totalWeeks, required this.phases, required this.weeks, this.author, this.isImported = false, DateTime? createdAt, }) : createdAt = createdAt ?? DateTime.now(); /// Returns the phase that contains [weekNumber], or null. TrainingPhase? phaseForWeek(int weekNumber) { for (final phase in phases) { if (weekNumber >= phase.startWeek && weekNumber <= phase.endWeek) { return phase; } } return null; } /// Number of training days across the entire program. int get totalDays => weeks.fold(0, (sum, w) => sum + w.days.length); Map toJson() => { 'id': id, 'name': name, 'description': description, 'totalWeeks': totalWeeks, 'phases': phases.map((p) => p.toJson()).toList(), 'weeks': weeks.map((w) => w.toJson()).toList(), 'author': author, 'isImported': isImported, 'createdAt': createdAt.toIso8601String(), }; factory TrainingProgram.fromJson(Map json) => TrainingProgram( id: json['id'] as String, name: json['name'] as String, description: json['description'] as String?, totalWeeks: json['totalWeeks'] as int, phases: (json['phases'] as List) .map((p) => TrainingPhase.fromJson(p as Map)) .toList(), weeks: (json['weeks'] as List) .map((w) => ProgramWeek.fromJson(w as Map)) .toList(), author: json['author'] as String?, isImported: json['isImported'] as bool? ?? false, createdAt: json['createdAt'] != null ? DateTime.parse(json['createdAt'] as String) : DateTime.now(), ); TrainingProgram copyWith({ Object? id = _sentinel, Object? name = _sentinel, Object? description = _sentinel, Object? totalWeeks = _sentinel, Object? phases = _sentinel, Object? weeks = _sentinel, Object? author = _sentinel, Object? isImported = _sentinel, Object? createdAt = _sentinel, }) => TrainingProgram( id: id == _sentinel ? this.id : id as String, name: name == _sentinel ? this.name : name as String, description: description == _sentinel ? this.description : description as String?, totalWeeks: totalWeeks == _sentinel ? this.totalWeeks : totalWeeks as int, phases: phases == _sentinel ? this.phases : phases as List, weeks: weeks == _sentinel ? this.weeks : weeks as List, author: author == _sentinel ? this.author : author as String?, isImported: isImported == _sentinel ? this.isImported : isImported as bool, createdAt: createdAt == _sentinel ? this.createdAt : createdAt as DateTime, ); } // ==================== AI Coach Chat ==================== /// A single message in an AI coach conversation. class ChatMessage { final String id; final String role; // 'user' | 'model' final String text; final DateTime timestamp; // Names of tools the model called (in order) while producing this reply. // Null/empty for user messages and replies that used no tools. final List? toolCalls; final String? imageBytesBase64; final String? imageMimeType; final WebSearchResult? webSearch; ChatMessage({ String? id, required this.role, required this.text, DateTime? timestamp, this.toolCalls, this.imageBytesBase64, this.imageMimeType, this.webSearch, }) : id = id ?? _uuid.v4(), timestamp = timestamp ?? DateTime.now(); Map toJson() => { 'id': id, 'role': role, 'text': text, 'timestamp': timestamp.toIso8601String(), if (toolCalls != null && toolCalls!.isNotEmpty) 'toolCalls': toolCalls, if (imageBytesBase64 != null) 'imageBytesBase64': imageBytesBase64, if (imageMimeType != null) 'imageMimeType': imageMimeType, if (webSearch != null) 'webSearch': webSearch!.toJson(), }; factory ChatMessage.fromJson(Map json) => ChatMessage( id: json['id'] as String?, role: json['role'] as String, text: json['text'] as String, timestamp: DateTime.parse(json['timestamp'] as String), toolCalls: (json['toolCalls'] as List?)?.cast(), imageBytesBase64: json['imageBytesBase64'] as String?, imageMimeType: json['imageMimeType'] as String?, webSearch: json['webSearch'] == null ? null : WebSearchResult.fromJson( Map.from(json['webSearch'] as Map), ), ); ChatMessage copyWith({ Object? role = _sentinel, Object? text = _sentinel, Object? timestamp = _sentinel, Object? toolCalls = _sentinel, Object? imageBytesBase64 = _sentinel, Object? imageMimeType = _sentinel, Object? webSearch = _sentinel, }) => ChatMessage( id: id, webSearch: webSearch == _sentinel ? this.webSearch : webSearch as WebSearchResult?, role: role == _sentinel ? this.role : role as String, text: text == _sentinel ? this.text : text as String, timestamp: timestamp == _sentinel ? this.timestamp : timestamp as DateTime, toolCalls: toolCalls == _sentinel ? this.toolCalls : toolCalls as List?, imageBytesBase64: imageBytesBase64 == _sentinel ? this.imageBytesBase64 : imageBytesBase64 as String?, imageMimeType: imageMimeType == _sentinel ? this.imageMimeType : imageMimeType as String?, ); } /// A persisted AI coach conversation: an ordered list of [ChatMessage]s. class Conversation { final String id; final String title; final String kind; // 'coach' | 'optimizer' final DateTime createdAt; final DateTime updatedAt; final List messages; Conversation({ String? id, required this.title, this.kind = 'coach', DateTime? createdAt, DateTime? updatedAt, List? messages, }) : id = id ?? _uuid.v4(), createdAt = createdAt ?? DateTime.now(), updatedAt = updatedAt ?? createdAt ?? DateTime.now(), messages = messages ?? const []; Map toJson() => { 'id': id, 'title': title, 'kind': kind, 'createdAt': createdAt.toIso8601String(), 'updatedAt': updatedAt.toIso8601String(), 'messages': messages.map((m) => m.toJson()).toList(), }; factory Conversation.fromJson(Map json) => Conversation( id: json['id'] as String?, title: json['title'] as String, kind: json['kind'] as String? ?? 'coach', createdAt: DateTime.parse(json['createdAt'] as String), updatedAt: json['updatedAt'] != null ? DateTime.parse(json['updatedAt'] as String) : DateTime.parse(json['createdAt'] as String), messages: (json['messages'] as List) .map((m) => ChatMessage.fromJson(m as Map)) .toList(), ); Conversation copyWith({ Object? title = _sentinel, Object? kind = _sentinel, Object? createdAt = _sentinel, Object? updatedAt = _sentinel, Object? messages = _sentinel, }) => Conversation( id: id, title: title == _sentinel ? this.title : title as String, kind: kind == _sentinel ? this.kind : kind as String, createdAt: createdAt == _sentinel ? this.createdAt : createdAt as DateTime, updatedAt: updatedAt == _sentinel ? this.updatedAt : updatedAt as DateTime, messages: messages == _sentinel ? this.messages : messages as List, ); } // ==================== AI Question Models ==================== /// One question the AI asks the user, with predefined options. class QuestionSpec { final String question; final List options; final bool multiSelect; final bool allowCustom; const QuestionSpec({ required this.question, required this.options, this.multiSelect = false, this.allowCustom = true, }); factory QuestionSpec.fromJson(Map j) => QuestionSpec( question: j['question'] as String, options: (j['options'] as List).cast(), multiSelect: j['multiSelect'] as bool? ?? false, allowCustom: j['allowCustom'] as bool? ?? true, ); } /// The user's answer to one [QuestionSpec]. class AnswerSpec { final String question; final List selected; final String? custom; const AnswerSpec({ required this.question, required this.selected, this.custom, }); Map toJson() => { 'question': question, 'selected': selected, if (custom != null && custom!.isNotEmpty) 'custom': custom, }; } /// The structured payload from an `ask_user_questions` tool call. class PendingQuestions { final String? preamble; final List questions; const PendingQuestions({this.preamble, required this.questions}); factory PendingQuestions.fromJson(Map j) => PendingQuestions( preamble: j['preamble'] as String?, questions: (j['questions'] as List) .map((q) => QuestionSpec.fromJson(q as Map)) .toList(), ); } // ==================== Readiness ==================== /// Coarse training-readiness classification derived from [ReadinessSnapshot]. enum ReadinessBand { high, moderate, low } /// A single point-in-time health measurement read from Health Connect. class HealthSample { final DateTime time; final double value; const HealthSample({required this.time, required this.value}); } /// One continuous sleep-stage segment within a `SleepPeriod`. /// /// Stage is one of: `'deep'`, `'rem'`, `'light'`, `'awake'`. class SleepStageInterval { final DateTime start; final DateTime end; final String stage; const SleepStageInterval({ required this.start, required this.end, required this.stage, }); } /// A sleep session interval read from Health Connect. /// /// When stage data is available (from `SleepSessionRecord.samples`), /// `lightMinutes`, `deepMinutes`, `remMinutes`, and `awakeMinutes` are /// populated and `minutes` returns actual sleep time (light + deep + rem), /// excluding awake/out-of-bed spans. Without stage data `minutes` falls back /// to the raw session duration. /// /// `stageTimeline` carries the ordered list of stage segments when available, /// used by the Sleep HR chart to colour-code each 10-minute bar. class SleepPeriod { final DateTime start; final DateTime end; /// Minutes in light (or unspecified) sleep. Null when no stage data. final int? lightMinutes; final int? deepMinutes; final int? remMinutes; /// Awake/out-of-bed minutes within the session window. final int? awakeMinutes; /// Ordered stage segments, populated from `SleepSessionRecord.samples`. /// Empty when the session record carries no stage breakdown. final List stageTimeline; const SleepPeriod({ required this.start, required this.end, this.lightMinutes, this.deepMinutes, this.remMinutes, this.awakeMinutes, this.stageTimeline = const [], }); bool get hasStages => lightMinutes != null || deepMinutes != null || remMinutes != null; /// Actual sleep minutes: light + deep + rem when stage data exists, /// otherwise the raw session span (start → end). int get minutes => hasStages ? (lightMinutes ?? 0) + (deepMinutes ?? 0) + (remMinutes ?? 0) : end.difference(start).inMinutes; } /// Rolling per-component averages used as the personal reference point /// when scoring today's readiness. Recomputed at most once per day. class ReadinessBaseline { final String dateKey; // yyyy-MM-dd the baseline was computed for final double? avgSleepMinutes; final int sleepNights; final double? avgRestingHr; final int rhrDays; final double? avgHrvMs; final int hrvDays; const ReadinessBaseline({ required this.dateKey, this.avgSleepMinutes, this.sleepNights = 0, this.avgRestingHr, this.rhrDays = 0, this.avgHrvMs, this.hrvDays = 0, }); Map toJson() => { 'dateKey': dateKey, 'avgSleepMinutes': avgSleepMinutes, 'sleepNights': sleepNights, 'avgRestingHr': avgRestingHr, 'rhrDays': rhrDays, 'avgHrvMs': avgHrvMs, 'hrvDays': hrvDays, }; factory ReadinessBaseline.fromJson(Map json) => ReadinessBaseline( dateKey: json['dateKey'] as String, avgSleepMinutes: (json['avgSleepMinutes'] as num?)?.toDouble(), sleepNights: json['sleepNights'] as int? ?? 0, avgRestingHr: (json['avgRestingHr'] as num?)?.toDouble(), rhrDays: json['rhrDays'] as int? ?? 0, avgHrvMs: (json['avgHrvMs'] as num?)?.toDouble(), hrvDays: json['hrvDays'] as int? ?? 0, ); } /// One day's computed readiness with the per-component evidence behind it. /// /// Any component (sleep / resting HR / HRV) may be null when the data or a /// reliable baseline is unavailable; [score] is null when no component could /// be scored at all, in which case the UI hides readiness entirely. class ReadinessSnapshot { final String dateKey; // yyyy-MM-dd this snapshot describes final int? score; // 0–100 overall, null = nothing scorable final ReadinessBand? band; final int? sleepMinutes; final double? sleepBaselineMinutes; final int? sleepScore; final double? restingHr; final double? rhrBaseline; final int? rhrScore; final double? hrvMs; final double? hrvBaseline; final int? hrvScore; final DateTime computedAt; ReadinessSnapshot({ required this.dateKey, this.score, this.band, this.sleepMinutes, this.sleepBaselineMinutes, this.sleepScore, this.restingHr, this.rhrBaseline, this.rhrScore, this.hrvMs, this.hrvBaseline, this.hrvScore, DateTime? computedAt, }) : computedAt = computedAt ?? DateTime.now(); Map toJson() => { 'dateKey': dateKey, 'score': score, 'band': band?.name, 'sleepMinutes': sleepMinutes, 'sleepBaselineMinutes': sleepBaselineMinutes, 'sleepScore': sleepScore, 'restingHr': restingHr, 'rhrBaseline': rhrBaseline, 'rhrScore': rhrScore, 'hrvMs': hrvMs, 'hrvBaseline': hrvBaseline, 'hrvScore': hrvScore, 'computedAt': computedAt.toIso8601String(), }; factory ReadinessSnapshot.fromJson(Map json) => ReadinessSnapshot( dateKey: json['dateKey'] as String, score: json['score'] as int?, band: json['band'] != null ? ReadinessBand.values.byName(json['band'] as String) : null, sleepMinutes: json['sleepMinutes'] as int?, sleepBaselineMinutes: (json['sleepBaselineMinutes'] as num?) ?.toDouble(), sleepScore: json['sleepScore'] as int?, restingHr: (json['restingHr'] as num?)?.toDouble(), rhrBaseline: (json['rhrBaseline'] as num?)?.toDouble(), rhrScore: json['rhrScore'] as int?, hrvMs: (json['hrvMs'] as num?)?.toDouble(), hrvBaseline: (json['hrvBaseline'] as num?)?.toDouble(), hrvScore: json['hrvScore'] as int?, computedAt: DateTime.parse(json['computedAt'] as String), ); }