import 'package:equatable/equatable.dart';
import 'package:slrpg_app/src/shared/domain/entities/exercise.dart';
import 'package:slrpg_app/src/shared/domain/entities/workout_set.dart';

class BattleState extends Equatable {
  final List<Exercise> exercises;
  final int currentExerciseIndex;
  final int currentSetIndex;
  final int repsCompleted;
  final bool isLoading;
  final bool isResting;
  final int restSeconds;
  final BattleStatus status;
  final String? errorMessage;

  const BattleState({
    this.exercises = const [],
    this.currentExerciseIndex = 0,
    this.currentSetIndex = 0,
    this.repsCompleted = 0,
    this.isLoading = true,
    this.isResting = false,
    this.restSeconds = 0,
    this.status = BattleStatus.initial,
    this.errorMessage,
  });

  // Computed properties
  Exercise? get currentExercise {
    if (exercises.isEmpty || currentExerciseIndex >= exercises.length) {
      return null;
    }
    return exercises[currentExerciseIndex];
  }

  WorkoutSet? get currentSet {
    final exercise = currentExercise;
    if (exercise == null || currentSetIndex >= exercise.sets.length) {
      return null;
    }
    return exercise.sets[currentSetIndex];
  }

  bool get hasNextSet {
    final exercise = currentExercise;
    if (exercise == null) return false;
    return currentSetIndex < exercise.sets.length - 1;
  }

  bool get hasNextExercise {
    return currentExerciseIndex < exercises.length - 1;
  }

  int get totalHP {
    return exercises.fold<int>(
      0,
      (sum, ex) => sum + ex.sets.fold<int>(0, (s, set) => s + set.repsTarget),
    );
  }

  int get completedHP {
    return exercises.take(currentExerciseIndex).fold<int>(
              0,
              (sum, ex) =>
                  sum + ex.sets.fold<int>(0, (s, set) => s + set.repsActual),
            ) +
        (currentExercise?.sets
                .take(currentSetIndex)
                .fold<int>(0, (sum, set) => sum + set.repsActual) ??
            0);
  }

  BattleState copyWith({
    List<Exercise>? exercises,
    int? currentExerciseIndex,
    int? currentSetIndex,
    int? repsCompleted,
    bool? isLoading,
    bool? isResting,
    int? restSeconds,
    BattleStatus? status,
    String? errorMessage,
  }) {
    return BattleState(
      exercises: exercises ?? this.exercises,
      currentExerciseIndex: currentExerciseIndex ?? this.currentExerciseIndex,
      currentSetIndex: currentSetIndex ?? this.currentSetIndex,
      repsCompleted: repsCompleted ?? this.repsCompleted,
      isLoading: isLoading ?? this.isLoading,
      isResting: isResting ?? this.isResting,
      restSeconds: restSeconds ?? this.restSeconds,
      status: status ?? this.status,
      errorMessage: errorMessage ?? this.errorMessage,
    );
  }

  @override
  List<Object?> get props => [
        exercises,
        currentExerciseIndex,
        currentSetIndex,
        repsCompleted,
        isLoading,
        isResting,
        restSeconds,
        status,
        errorMessage,
      ];
}

enum BattleStatus {
  initial,
  loading,
  ready,
  resting,
  completed,
  error,
}
