import 'package:equatable/equatable.dart';
import '../../data/datasources/quiz_remote_datasource.dart';

sealed class QuizState extends Equatable {
  const QuizState();
  @override
  List<Object?> get props => [];
}

class QuizSetup extends QuizState {
  final FilterOptionsModel? filters;
  final bool isLoadingFilters;

  const QuizSetup({this.filters, this.isLoadingFilters = false});

  @override
  List<Object?> get props => [filters, isLoadingFilters];
}

class QuizStarting extends QuizState {
  const QuizStarting();
}

class QuizActive extends QuizState {
  final List<QuizQuestionModel> questions;
  final int currentIndex;
  final List<String?> userAnswers; // label chosen per question, null = unanswered
  final bool currentRevealed;
  final int attemptId;

  const QuizActive({
    required this.questions,
    required this.currentIndex,
    required this.userAnswers,
    required this.currentRevealed,
    required this.attemptId,
  });

  QuizQuestionModel get currentQuestion => questions[currentIndex];
  int get totalQuestions => questions.length;
  bool get isLastQuestion => currentIndex == questions.length - 1;
  int get answeredCount => userAnswers.where((a) => a != null).length;

  QuizActive copyWith({
    int? currentIndex,
    List<String?>? userAnswers,
    bool? currentRevealed,
  }) {
    return QuizActive(
      questions: questions,
      currentIndex: currentIndex ?? this.currentIndex,
      userAnswers: userAnswers ?? this.userAnswers,
      currentRevealed: currentRevealed ?? this.currentRevealed,
      attemptId: attemptId,
    );
  }

  @override
  List<Object?> get props =>
      [currentIndex, userAnswers, currentRevealed, attemptId];
}

class QuizSubmitting extends QuizState {
  const QuizSubmitting();
}

class QuizResults extends QuizState {
  final QuizSubmitResultModel result;
  final List<QuizQuestionModel> questions;
  final List<String?> userAnswers;

  const QuizResults({
    required this.result,
    required this.questions,
    required this.userAnswers,
  });

  @override
  List<Object?> get props => [result.score];
}

class QuizError extends QuizState {
  final String message;
  const QuizError(this.message);

  @override
  List<Object?> get props => [message];
}