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.Ending;
import thb.jeanluc.adventure.model.Player;
import thb.jeanluc.adventure.model.Room;
import thb.jeanluc.adventure.model.World;

import java.util.List;
import java.util.Map;

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

class EndingEngineTest {
    private GameContext ctx(List<Ending> endings) {
        Player p = new Player(new Room("k", "K", "d"), 0);
        World w = new World(Map.of(), Map.of(), Map.of(), "t", "w", Map.of(), endings);
        return new GameContext(w, p, new TestIO());
    }

    private Ending ending(String id, boolean victory, String flag) {
        return new Ending(id, id + "-title", victory,
                List.of(new Condition(Condition.Type.FLAG, flag)), id + " text");
    }

    @Test
    void firstMatchingEndingWinsByOrder() {
        GameContext ctx = ctx(List.of(ending("victory", true, "won"), ending("fled", false, "fled")));
        ctx.getState().set("won");
        ctx.getState().set("fled");
        assertThat(EndingEngine.triggered(ctx).id()).isEqualTo("victory");
    }

    @Test
    void noEndingWhenNoConditionHolds() {
        GameContext ctx = ctx(List.of(ending("victory", true, "won")));
        assertThat(EndingEngine.triggered(ctx)).isNull();
    }

    @Test
    void renderIncludesTitleTextAndSummary() {
        GameContext ctx = ctx(List.of(ending("victory", true, "won")));
        ctx.getState().set("won");
        String out = EndingEngine.render(EndingEngine.triggered(ctx), ctx, 7).plainText();
        assertThat(out).contains("victory-title").contains("victory text");
        assertThat(out).contains("Turns: 7").contains("Quests completed: 0 / 0");
    }
}
