package thb.jeanluc.adventure.game;

import org.junit.jupiter.api.Test;
import thb.jeanluc.adventure.io.TestIO;
import thb.jeanluc.adventure.model.Condition;
import thb.jeanluc.adventure.model.Player;
import thb.jeanluc.adventure.model.Quest;
import thb.jeanluc.adventure.model.QuestStage;
import thb.jeanluc.adventure.model.Room;
import thb.jeanluc.adventure.model.World;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;

class QuestAutosaveTest {

    @Test
    void questCompletionTriggersSaveCallback() {
        Quest q = new Quest("q", "Quest", true, List.of(
                new QuestStage("Do it", List.of(new Condition(Condition.Type.FLAG, "a")), List.of())),
                List.of());
        Room k = new Room("k", "K", "d");
        World w = new World(Map.of("k", k), Map.of(), Map.of(), "t", "w", Map.of("q", q));
        GameContext ctx = new GameContext(new GameSession(w, new Player(k, 0), "slot"), new TestIO());
        AtomicInteger saves = new AtomicInteger();
        ctx.setSaveCallback(saves::incrementAndGet);

        QuestEngine.tick(ctx);          // starts quest; not complete yet
        assertThat(saves.get()).isZero();
        ctx.getState().set("a");
        QuestEngine.tick(ctx);          // completes quest → autosave
        assertThat(saves.get()).isEqualTo(1);
    }
}
