import { UIFlowMgr, ISceneEnter } from '@ui/UIFlowMgr';
import { StorageMgr } from '@common/StorageMgr';
import { STORAGE_KEY } from '@common/Constants';

function mem() {
    const m = new Map<string, string>();
    return {
        getItem: (k: string) => (m.has(k) ? (m.get(k) as string) : null),
        setItem: (k: string, v: string) => {
            m.set(k, v);
        },
        removeItem: (k: string) => {
            m.delete(k);
        },
    };
}

describe('UIFlowMgr — boot path (req 19.1, 19.5)', () => {
    it('routes first-time boot into story_intro', () => {
        const events: ISceneEnter[] = [];
        const flow = new UIFlowMgr(new StorageMgr(mem()), { onSceneEnter: (e) => events.push(e) });
        flow.onBoot();
        expect(events[0].scene).toBe('story_intro');
    });

    it('routes repeat boot into main_menu', () => {
        const storage = new StorageMgr(mem());
        storage.set(STORAGE_KEY.StoryIntroSeen, true);
        const events: ISceneEnter[] = [];
        const flow = new UIFlowMgr(storage, { onSceneEnter: (e) => events.push(e) });
        flow.onBoot();
        expect(events[0].scene).toBe('main_menu');
    });
});

describe('UIFlowMgr — level/settlement/dead transitions', () => {
    it('onPickLevel → gameplay with payload', () => {
        const events: ISceneEnter[] = [];
        const flow = new UIFlowMgr(new StorageMgr(mem()), { onSceneEnter: (e) => events.push(e) });
        flow.onPickLevel('1-3');
        expect(events[0]).toEqual({ scene: 'gameplay', payload: { levelId: '1-3' } });
    });

    it('onPlayerDied pushes a settlement with dead:true', () => {
        const events: ISceneEnter[] = [];
        const flow = new UIFlowMgr(new StorageMgr(mem()), { onSceneEnter: (e) => events.push(e) });
        flow.onPlayerDied('1-2');
        expect(events[0].scene).toBe('settlement');
        expect(events[0].payload).toEqual({ levelId: '1-2', dead: true });
    });
});

describe('UIFlowMgr — difficulty guardrail (req 13.1)', () => {
    it('availableSettingsEntries does NOT include difficulty', () => {
        const flow = new UIFlowMgr(new StorageMgr(mem()));
        const entries = flow.availableSettingsEntries();
        expect(entries).not.toContain('difficulty');
        expect(entries).toContain('audio_volume');
        expect(entries).toContain('replay_story_intro');
    });
});
