import 'package:asciineverdie/src/core/model/game_state.dart';
import 'package:asciineverdie/src/core/model/save_data.dart';
import 'package:asciineverdie/src/core/storage/save_repository.dart';
import 'package:asciineverdie/src/core/storage/save_service.dart'
    show SaveFileInfo;

/// Coordinates saving/loading GameState using SaveRepository.
class SaveManager {
  SaveManager(this._repo);

  final SaveRepository _repo;
  static const String defaultFileName = 'progress.pqf';

  /// Save current game state to disk. [fileName] may be absolute or relative.
  /// Returns outcome with error on failure.
  Future<SaveOutcome> saveState(GameState state, {String? fileName}) {
    final save = GameSave.fromState(state);
    return _repo.save(save, fileName ?? defaultFileName);
  }

  /// Load game state from disk. [fileName] may be absolute (e.g., file picker).
  /// Returns outcome + optional state.
  Future<(SaveOutcome, GameState?)> loadState({String? fileName}) async {
    final (outcome, save) = await _repo.load(fileName ?? defaultFileName);
    if (!outcome.success || save == null) {
      return (outcome, null);
    }
    return (outcome, save.toState());
  }

  /// 저장 파일 목록 조회
  Future<List<SaveFileInfo>> listSaves() => _repo.listSaves();

  /// 저장 파일 삭제
  Future<SaveOutcome> deleteSave({String? fileName}) {
    return _repo.deleteSave(fileName ?? defaultFileName);
  }

  /// 저장 파일 존재 여부 확인
  Future<bool> saveExists({String? fileName}) {
    return _repo.saveExists(fileName ?? defaultFileName);
  }
}
