import 'enums.dart';

class UserProfile {
  final int age;
  final Sex sex;
  final String country;
  final double heightCm;
  final double weightKg;
  final Set<Diagnosis> diagnoses;

  const UserProfile({
    required this.age,
    required this.sex,
    required this.country,
    required this.heightCm,
    required this.weightKg,
    this.diagnoses = const {},
  });

  double get bmi => weightKg / ((heightCm / 100) * (heightCm / 100));

  Map<String, dynamic> toJson() => {
        'age': age,
        'sex': sex.name,
        'country': country,
        'heightCm': heightCm,
        'weightKg': weightKg,
        'diagnoses': diagnoses.map((d) => d.name).toList(),
      };

  factory UserProfile.fromJson(Map<String, dynamic> json) => UserProfile(
        age: json['age'] as int,
        sex: Sex.values.byName(json['sex'] as String),
        country: json['country'] as String,
        heightCm: (json['heightCm'] as num).toDouble(),
        weightKg: (json['weightKg'] as num).toDouble(),
        diagnoses: (json['diagnoses'] as List<dynamic>)
            .map((d) => Diagnosis.values.byName(d as String))
            .toSet(),
      );

  UserProfile copyWith({
    int? age,
    Sex? sex,
    String? country,
    double? heightCm,
    double? weightKg,
    Set<Diagnosis>? diagnoses,
  }) =>
      UserProfile(
        age: age ?? this.age,
        sex: sex ?? this.sex,
        country: country ?? this.country,
        heightCm: heightCm ?? this.heightCm,
        weightKg: weightKg ?? this.weightKg,
        diagnoses: diagnoses ?? this.diagnoses,
      );
}
