class UserStatsModel {
  final int totalScore;
  final int quizzesTaken;
  final int avgScore;
  final int totalQuestionsAnswered;
  final int totalCorrect;
  final double accuracy;
  final List<QuizAttemptModel> recentAttempts;
  final List<CategoryBreakdownModel> categories;

  const UserStatsModel({
    this.totalScore = 0,
    this.quizzesTaken = 0,
    this.avgScore = 0,
    this.totalQuestionsAnswered = 0,
    this.totalCorrect = 0,
    this.accuracy = 0,
    this.recentAttempts = const [],
    this.categories = const [],
  });

  factory UserStatsModel.fromJson(Map<String, dynamic> json) {
    return UserStatsModel(
      totalScore: (json['total_score'] as num?)?.toInt() ?? 0,
      quizzesTaken: (json['quizzes_taken'] as num?)?.toInt() ?? 0,
      avgScore: (json['avg_score'] as num?)?.toInt() ?? 0,
      totalQuestionsAnswered:
          (json['total_questions_answered'] as num?)?.toInt() ?? 0,
      totalCorrect: (json['total_correct'] as num?)?.toInt() ?? 0,
      accuracy: (json['accuracy'] as num?)?.toDouble() ?? 0,
      recentAttempts: (json['recent_attempts'] as List<dynamic>?)
              ?.map((e) =>
                  QuizAttemptModel.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      categories: (json['categories'] as List<dynamic>?)
              ?.map((e) =>
                  CategoryBreakdownModel.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }
}

class QuizAttemptModel {
  final int id;
  final int? score;
  final int? correctAnswers;
  final int? totalQuestions;
  final String status;
  final String? startedAt;
  final String? completedAt;

  const QuizAttemptModel({
    required this.id,
    this.score,
    this.correctAnswers,
    this.totalQuestions,
    required this.status,
    this.startedAt,
    this.completedAt,
  });

  factory QuizAttemptModel.fromJson(Map<String, dynamic> json) {
    return QuizAttemptModel(
      id: (json['id'] as num).toInt(),
      score: (json['score'] as num?)?.toInt(),
      correctAnswers: (json['correct_answers'] as num?)?.toInt(),
      totalQuestions: (json['total_questions'] as num?)?.toInt(),
      status: (json['status'] as String?) ?? 'unknown',
      startedAt: json['started_at'] as String?,
      completedAt: json['completed_at'] as String?,
    );
  }
}

class CategoryBreakdownModel {
  final String category;
  final int attempts;
  final int correct;

  const CategoryBreakdownModel({
    required this.category,
    required this.attempts,
    this.correct = 0,
  });

  factory CategoryBreakdownModel.fromJson(Map<String, dynamic> json) {
    return CategoryBreakdownModel(
      category: json['category'] as String,
      attempts: (json['attempts'] as num).toInt(),
      correct: (json['correct'] as num?)?.toInt() ?? 0,
    );
  }
}