// ABOUTME: Blood pressure entry model for tracking blood pressure measurements  
// ABOUTME: Extends health_data base class and includes systolic/diastolic values with medical categorization
import 'package:albi/models/health_data.dart';
import 'package:albi/utils/constants.dart';

/// Represents a blood pressure measurement entry in the health tracker
class BloodPressureEntry extends HealthData {
  /// Systolic blood pressure (top number) in mmHg
  final int systolic;
  
  /// Diastolic blood pressure (bottom number) in mmHg  
  final int diastolic;

  const BloodPressureEntry({
    super.id,
    required super.timestamp,
    super.notes,
    required this.systolic,
    required this.diastolic,
  });

  /// Create a BloodPressureEntry from database JSON
  factory BloodPressureEntry.fromJson(Map<String, dynamic> json) {
    return BloodPressureEntry(
      id: json['id'] as int?,
      timestamp: HealthData.parseTimestamp(json['timestamp'] as String),
      notes: json['notes'] as String?,
      systolic: json['systolic'] as int,
      diastolic: json['diastolic'] as int,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'timestamp': timestampAsString,
      'notes': notes,
      'systolic': systolic,
      'diastolic': diastolic,
    };
  }

  @override
  BloodPressureEntry copyWith({
    int? id,
    DateTime? timestamp,
    String? notes,
    int? systolic,
    int? diastolic,
  }) {
    return BloodPressureEntry(
      id: id ?? this.id,
      timestamp: timestamp ?? this.timestamp,
      notes: notes ?? this.notes,
      systolic: systolic ?? this.systolic,
      diastolic: diastolic ?? this.diastolic,
    );
  }

  /// Get the blood pressure category based on medical guidelines
  String get category {
    // Based on American Heart Association guidelines
    if (systolic >= 180 || diastolic >= 120) {
      return 'crisis';
    } else if (systolic >= 140 || diastolic >= 90) {
      return 'high_stage_2';
    } else if (systolic >= 130 || diastolic >= 80) {
      return 'high_stage_1';
    } else if (systolic >= 120 && diastolic < 80) {
      return 'elevated';
    } else if (systolic < 120 && diastolic < 80) {
      return 'normal';
    } else {
      return 'high_stage_1'; // Default for edge cases
    }
  }

  /// Get a human-readable category name
  String get categoryDisplayName {
    switch (category) {
      case 'normal':
        return 'Normal';
      case 'elevated':
        return 'Elevated';
      case 'high_stage_1':
        return 'High Stage 1';
      case 'high_stage_2':
        return 'High Stage 2';
      case 'crisis':
        return 'Hypertensive Crisis';
      default:
        return 'Unknown';
    }
  }

  /// Get the color value associated with the blood pressure category
  int get categoryColorValue {
    switch (category) {
      case 'normal':
        return 0xFF4CAF50; // Green - success
      case 'elevated':
        return 0xFFFF9800; // Orange - warning
      case 'high_stage_1':
        return 0xFFFF9800; // Orange - warning
      case 'high_stage_2':
        return 0xFFE53935; // Red - error
      case 'crisis':
        return 0xFF8E24AA; // Deep purple for crisis
      default:
        return 0xFFE53935; // Red - error
    }
  }

  /// Check if this blood pressure reading indicates a medical emergency
  bool get isEmergency => category == 'crisis';

  /// Check if this blood pressure reading is considered high
  bool get isHigh => category.startsWith('high') || category == 'crisis';

  /// Format the blood pressure reading for display
  String get formatted => '$systolic/$diastolic ${UnitConstants.mmHgUnit}';

  /// Get a detailed description of this blood pressure reading
  String get description {
    final categoryName = categoryDisplayName;
    return '$formatted - $categoryName';
  }

  /// Validate the blood pressure entry
  bool get isValid {
    return isValidTimestamp && 
           isValidNotes && 
           systolic >= HealthDefaults.minBloodPressure && 
           systolic <= HealthDefaults.maxSystolic &&
           diastolic >= HealthDefaults.minBloodPressure && 
           diastolic <= HealthDefaults.maxDiastolic &&
           systolic > diastolic; // Systolic should always be higher
  }

  /// Calculate pulse pressure (difference between systolic and diastolic)
  int get pulsePressure => systolic - diastolic;

  /// Calculate mean arterial pressure (estimate)
  double get meanArterialPressure => diastolic + (pulsePressure / 3);

  @override
  List<Object?> get props => [
    ...super.props,
    systolic,
    diastolic,
  ];

  @override
  String toString() {
    return 'BloodPressureEntry(id: $id, timestamp: $timestamp, systolic: $systolic, diastolic: $diastolic, category: $category)';
  }
}
