package thb.jeanluc.adventure;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import thb.jeanluc.adventure.io.TestIO;
import thb.jeanluc.adventure.save.SaveService;
import thb.jeanluc.adventure.save.SettingsStore;

import java.nio.file.Path;
import java.util.List;

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

/**
 * Regression test for the main-menu Quit: choosing Quit must end the menu loop
 * AND tell the frontend to tear down. Previously {@code run} just returned,
 * which exits the console but left the Swing window open (the menu overlay
 * closed and the game reappeared underneath). We assert the frontend-agnostic
 * contract: the Quit path invokes {@link thb.jeanluc.adventure.io.GameIO#shutdown()}.
 */
class AppQuitTest {

    /** Fake IO that always picks the last menu option (Quit) and records shutdown. */
    private static final class QuitIO extends TestIO {
        boolean shutdownCalled;
        int chooseCalls;

        @Override
        public int choose(String title, List<String> options) {
            chooseCalls++;
            return options.size() - 1; // Quit is the last main-menu option
        }

        @Override
        public void shutdown() {
            shutdownCalled = true;
        }
    }

    @Test
    void choosingQuit_tearsDownTheFrontend(@TempDir Path tmp) {
        QuitIO io = new QuitIO();
        SaveService saves = new SaveService(tmp.resolve("saves"));
        SettingsStore settings = new SettingsStore(tmp.resolve("settings.json"));

        App.run(io, saves, settings);

        assertThat(io.shutdownCalled).isTrue();
        assertThat(io.chooseCalls).isEqualTo(1); // straight to Quit, no game started
        assertThat(io.allOutput()).contains("Farewell.");
    }
}
