import 'enums.dart'; class LifespanDelta { final int lowMonths; final int highMonths; final Confidence confidence; const LifespanDelta({ required this.lowMonths, required this.highMonths, required this.confidence, }); int get midpointMonths => ((lowMonths + highMonths) / 2).round(); String get rangeDisplay { if (lowMonths <= 0 && highMonths <= 0) return '0'; if (lowMonths == highMonths) return '$lowMonths'; return '$lowMonths–$highMonths'; } Map toJson() => { 'lowMonths': lowMonths, 'highMonths': highMonths, 'confidence': confidence.name, }; factory LifespanDelta.fromJson(Map json) => LifespanDelta( lowMonths: json['lowMonths'] as int, highMonths: json['highMonths'] as int, confidence: Confidence.values.byName(json['confidence'] as String), ); } class RankedFactor { final String behaviorKey; final String displayName; final LifespanDelta delta; const RankedFactor({ required this.behaviorKey, required this.displayName, required this.delta, }); Map toJson() => { 'behaviorKey': behaviorKey, 'displayName': displayName, 'delta': delta.toJson(), }; factory RankedFactor.fromJson(Map json) => RankedFactor( behaviorKey: json['behaviorKey'] as String, displayName: json['displayName'] as String, delta: LifespanDelta.fromJson(json['delta'] as Map), ); } class CalculationResult { final List rankedFactors; final String modelVersion; final DateTime calculatedAt; const CalculationResult({ required this.rankedFactors, required this.modelVersion, required this.calculatedAt, }); RankedFactor? get dominantFactor => rankedFactors.isNotEmpty ? rankedFactors.first : null; RankedFactor? get secondaryFactor => rankedFactors.length > 1 ? rankedFactors[1] : null; Map toJson() => { 'rankedFactors': rankedFactors.map((f) => f.toJson()).toList(), 'modelVersion': modelVersion, 'calculatedAt': calculatedAt.toIso8601String(), }; factory CalculationResult.fromJson(Map json) => CalculationResult( rankedFactors: (json['rankedFactors'] as List) .map((f) => RankedFactor.fromJson(f as Map)) .toList(), modelVersion: json['modelVersion'] as String, calculatedAt: DateTime.parse(json['calculatedAt'] as String), ); }