// ABOUTME: Mood entry model for tracking user mood and emotional state
// ABOUTME: Extends health_data base class and includes mood type with emoji representation
import 'package:albi/models/health_data.dart';
import 'package:albi/models/mood_type.dart';

/// Represents a mood tracking entry in the health tracker
class MoodEntry extends HealthData {
  /// The mood type selected by the user
  final MoodType mood;

  const MoodEntry({
    super.id,
    required super.timestamp,
    super.notes,
    required this.mood,
  });

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

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

  @override
  MoodEntry copyWith({
    int? id,
    DateTime? timestamp,
    String? notes,
    MoodType? mood,
  }) {
    return MoodEntry(
      id: id ?? this.id,
      timestamp: timestamp ?? this.timestamp,
      notes: notes ?? this.notes,
      mood: mood ?? this.mood,
    );
  }

  /// Get the emoji representation of this mood
  String get emoji => mood.emoji;

  /// Get the display name of this mood
  String get moodDisplayName => mood.displayName;

  /// Get the combined emoji and name label
  String get moodLabel => mood.label;

  /// Get the color value associated with this mood
  int get moodColorValue => mood.colorValue;

  /// Check if this is a positive mood
  bool get isPositiveMood {
    return mood == MoodType.energized || 
           mood == MoodType.calm;
  }

  /// Check if this is a negative mood
  bool get isNegativeMood {
    return mood == MoodType.stressed || 
           mood == MoodType.sad || 
           mood == MoodType.irritable;
  }

  /// Check if this is a neutral mood
  bool get isNeutralMood {
    return mood == MoodType.neutral || 
           mood == MoodType.tired;
  }

  /// Get mood category for analytics
  String get moodCategory {
    if (isPositiveMood) return 'positive';
    if (isNegativeMood) return 'negative';
    return 'neutral';
  }

  /// Get energy level associated with this mood (1-5 scale)
  int get energyLevel {
    switch (mood) {
      case MoodType.energized:
        return 5;
      case MoodType.calm:
        return 3;
      case MoodType.neutral:
        return 3;
      case MoodType.tired:
        return 1;
      case MoodType.stressed:
        return 2;
      case MoodType.sad:
        return 1;
      case MoodType.irritable:
        return 4;
      case MoodType.happy:
        return 4;
    }
  }

  /// Get valence (positive/negative feeling) on a 1-5 scale
  int get valence {
    switch (mood) {
      case MoodType.energized:
        return 5;
      case MoodType.calm:
        return 4;
      case MoodType.neutral:
        return 3;
      case MoodType.tired:
        return 2;
      case MoodType.stressed:
        return 1;
      case MoodType.sad:
        return 1;
      case MoodType.irritable:
        return 1;
      case MoodType.happy:
        return 5;
    }
  }

  /// Format the mood for display
  String get formatted => '$emoji $moodDisplayName';

  /// Get a detailed description including notes if available
  String get description {
    final base = formatted;
    if (notes != null && notes!.isNotEmpty) {
      return '$base - $notes';
    }
    return base;
  }

  /// Validate the mood entry
  bool get isValid {
    return isValidTimestamp && isValidNotes;
  }

  /// Get suggestions based on this mood entry
  List<String> get suggestions {
    switch (mood) {
      case MoodType.energized:
        return [
          'Great time for physical activity!',
          'Consider tackling challenging tasks',
          'Share your energy with others',
        ];
      case MoodType.calm:
        return [
          'Perfect time for meditation or reflection',
          'Good for creative activities',
          'Enjoy this peaceful moment',
        ];
      case MoodType.neutral:
        return [
          'A steady state - maintain your routine',
          'Good for planning and organizing',
          'Consider what might lift your spirits',
        ];
      case MoodType.tired:
        return [
          'Consider taking a rest or short nap',
          'Stay hydrated and eat well',
          'Early bedtime might help',
        ];
      case MoodType.stressed:
        return [
          'Try deep breathing exercises',
          'Take a short walk if possible',
          'Consider talking to someone',
        ];
      case MoodType.sad:
        return [
          'Reach out to supportive friends/family',
          'Engage in comforting activities',
          'Be gentle with yourself',
        ];
      case MoodType.irritable:
        return [
          'Take a few deep breaths',
          'Step away from stressful situations',
          'Physical activity might help release tension',
        ];
      case MoodType.happy:
        return [
          'Enjoy this positive moment!',
          'Share your joy with others',
          'Great time for social activities',
        ];
    }
  }

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

  @override
  String toString() {
    return 'MoodEntry(id: $id, timestamp: $timestamp, mood: ${mood.name}, emoji: $emoji)';
  }
}
