/// 게임 상태 모듈 (Game State Module)
///
/// 하위 파일들을 re-export하여 기존 import 호환성 유지
library;

export 'death_info.dart';
export 'equipment_container.dart';
export 'inventory.dart';
export 'progress_state.dart';
export 'queue_state.dart';
export 'skill_book.dart';
export 'skill_system_state.dart';
export 'stats.dart';
export 'task_info.dart';
export 'traits.dart';

import 'package:asciineverdie/src/core/model/death_info.dart';
import 'package:asciineverdie/src/core/model/equipment_container.dart';
import 'package:asciineverdie/src/core/model/inventory.dart';
import 'package:asciineverdie/src/core/model/potion.dart';
import 'package:asciineverdie/src/core/model/progress_state.dart';
import 'package:asciineverdie/src/core/model/queue_state.dart';
import 'package:asciineverdie/src/core/model/skill_book.dart';
import 'package:asciineverdie/src/core/model/skill_system_state.dart';
import 'package:asciineverdie/src/core/model/stats.dart';
import 'package:asciineverdie/src/core/model/traits.dart';
import 'package:asciineverdie/src/core/util/deterministic_random.dart';

/// 게임 전체 상태 (Game State)
///
/// Progress Quest 구조를 미러링하는 최소 스켈레톤 상태.
/// 로직은 Delphi 소스에서 충실하게 포팅됨.
class GameState {
  GameState({
    required DeterministicRandom rng,
    Traits? traits,
    Stats? stats,
    Inventory? inventory,
    Equipment? equipment,
    SkillBook? skillBook,
    ProgressState? progress,
    QueueState? queue,
    SkillSystemState? skillSystem,
    PotionInventory? potionInventory,
    this.deathInfo,
  }) : rng = DeterministicRandom.clone(rng),
       traits = traits ?? Traits.empty(),
       stats = stats ?? Stats.empty(),
       inventory = inventory ?? Inventory.empty(),
       equipment = equipment ?? Equipment.empty(),
       skillBook = skillBook ?? SkillBook.empty(),
       progress = progress ?? ProgressState.empty(),
       queue = queue ?? QueueState.empty(),
       skillSystem = skillSystem ?? SkillSystemState.empty(),
       potionInventory = potionInventory ?? const PotionInventory();

  factory GameState.withSeed({
    required int seed,
    Traits? traits,
    Stats? stats,
    Inventory? inventory,
    Equipment? equipment,
    SkillBook? skillBook,
    ProgressState? progress,
    QueueState? queue,
    SkillSystemState? skillSystem,
    PotionInventory? potionInventory,
    DeathInfo? deathInfo,
  }) {
    return GameState(
      rng: DeterministicRandom(seed),
      traits: traits,
      stats: stats,
      inventory: inventory,
      equipment: equipment,
      skillBook: skillBook,
      progress: progress,
      queue: queue,
      skillSystem: skillSystem,
      potionInventory: potionInventory,
      deathInfo: deathInfo,
    );
  }

  final DeterministicRandom rng;
  final Traits traits;
  final Stats stats;
  final Inventory inventory;
  final Equipment equipment;
  final SkillBook skillBook;
  final ProgressState progress;
  final QueueState queue;

  /// 스킬 시스템 상태 (Phase 3)
  final SkillSystemState skillSystem;

  /// 물약 인벤토리
  final PotionInventory potionInventory;

  /// 사망 정보 (Phase 4, null이면 생존 중)
  final DeathInfo? deathInfo;

  /// 사망 여부
  bool get isDead => deathInfo != null;

  GameState copyWith({
    DeterministicRandom? rng,
    Traits? traits,
    Stats? stats,
    Inventory? inventory,
    Equipment? equipment,
    SkillBook? skillBook,
    ProgressState? progress,
    QueueState? queue,
    SkillSystemState? skillSystem,
    PotionInventory? potionInventory,
    DeathInfo? deathInfo,
    bool clearDeathInfo = false,
  }) {
    return GameState(
      rng: rng ?? DeterministicRandom.clone(this.rng),
      traits: traits ?? this.traits,
      stats: stats ?? this.stats,
      inventory: inventory ?? this.inventory,
      equipment: equipment ?? this.equipment,
      skillBook: skillBook ?? this.skillBook,
      progress: progress ?? this.progress,
      queue: queue ?? this.queue,
      skillSystem: skillSystem ?? this.skillSystem,
      potionInventory: potionInventory ?? this.potionInventory,
      deathInfo: clearDeathInfo ? null : (deathInfo ?? this.deathInfo),
    );
  }
}
