/**
 * Jest unit tests for Re-run Research configuration handling.
 *
 * Tests the logic for:
 * 1. Building rerun config from research items (history.js handleRerun)
 * 2. Parsing and applying rerun config (research.js checkAndApplyRerunConfig)
 *
 * Note: Re-run only stores query and mode. All other settings (model, provider,
 * search engine, etc.) come from the user's current defaults via the settings manager.
 *
 * Run with: npx jest tests/infrastructure_tests/test_rerun_config.test.js
 */

// Mock sessionStorage
const mockSessionStorage = {
    store: {},
    getItem: jest.fn((key) => mockSessionStorage.store[key] || null),
    setItem: jest.fn((key, value) => { mockSessionStorage.store[key] = value; }),
    removeItem: jest.fn((key) => { delete mockSessionStorage.store[key]; }),
    clear: jest.fn(() => { mockSessionStorage.store = {}; })
};

// Mock URLValidator
const mockURLValidator = {
    safeAssign: jest.fn()
};

// Setup globals
global.sessionStorage = mockSessionStorage;
global.URLValidator = mockURLValidator;
global.window = {
    location: { href: '' }
};

// ============================================================================
// handleRerun Logic Tests (from history.js)
// ============================================================================

describe('handleRerun Configuration Building', () => {
    beforeEach(() => {
        jest.clearAllMocks();
        mockSessionStorage.clear();
    });

    /**
     * Simulates the handleRerun function logic.
     * Only stores query and mode - no advanced settings.
     */
    function buildRerunConfig(item) {
        return {
            query: item.query,
            mode: item.mode
        };
    }

    describe('Basic Config Building', () => {
        test('should include query and mode from item', () => {
            const item = {
                query: 'What is quantum computing?',
                mode: 'detailed'
            };

            const config = buildRerunConfig(item);

            expect(config.query).toBe('What is quantum computing?');
            expect(config.mode).toBe('detailed');
        });

        test('should handle missing mode gracefully', () => {
            const item = {
                query: 'Test query'
                // mode is undefined
            };

            const config = buildRerunConfig(item);

            expect(config.query).toBe('Test query');
            expect(config.mode).toBeUndefined();
        });

        test('should handle empty query', () => {
            const item = {
                query: '',
                mode: 'quick'
            };

            const config = buildRerunConfig(item);

            expect(config.query).toBe('');
            expect(config.mode).toBe('quick');
        });
    });

    describe('Advanced settings are NOT included', () => {
        test('should not include submission params even when present', () => {
            const item = {
                query: 'Test query',
                mode: 'detailed',
                metadata: {
                    submission: {
                        model_provider: 'OLLAMA',
                        model: 'llama3',
                        search_engine: 'duckduckgo',
                        iterations: 5,
                        questions_per_iteration: 3,
                        strategy: 'focused-iteration'
                    }
                }
            };

            const config = buildRerunConfig(item);

            expect(config.query).toBe('Test query');
            expect(config.mode).toBe('detailed');
            expect(config.model_provider).toBeUndefined();
            expect(config.model).toBeUndefined();
            expect(config.search_engine).toBeUndefined();
            expect(config.iterations).toBeUndefined();
            expect(config.questions_per_iteration).toBeUndefined();
            expect(config.strategy).toBeUndefined();
        });

        test('should only have query and mode keys', () => {
            const item = {
                query: 'Test query',
                mode: 'quick',
                metadata: { submission: { model_provider: 'OPENAI' } }
            };

            const config = buildRerunConfig(item);
            const keys = Object.keys(config);

            expect(keys).toEqual(['query', 'mode']);
        });
    });

});

// ============================================================================
// checkAndApplyRerunConfig Logic Tests (from research.js)
// ============================================================================

describe('checkAndApplyRerunConfig Parsing', () => {
    beforeEach(() => {
        jest.clearAllMocks();
        mockSessionStorage.clear();
    });

    /**
     * Simulates the config parsing logic from checkAndApplyRerunConfig
     */
    function parseRerunConfig() {
        const rerunConfigStr = sessionStorage.getItem('rerunConfig');
        if (!rerunConfigStr) return null;

        try {
            const config = JSON.parse(rerunConfigStr);
            sessionStorage.removeItem('rerunConfig');
            return config;
        } catch {
            sessionStorage.removeItem('rerunConfig');
            return null;
        }
    }

    describe('Valid Config Parsing', () => {
        test('should parse valid JSON config', () => {
            const config = { query: 'Test', mode: 'quick' };
            sessionStorage.setItem('rerunConfig', JSON.stringify(config));

            const parsed = parseRerunConfig();

            expect(parsed).toEqual(config);
            expect(sessionStorage.getItem('rerunConfig')).toBeNull();
        });

        test('should parse config with query and mode', () => {
            const config = {
                query: 'Full config test',
                mode: 'detailed'
            };
            sessionStorage.setItem('rerunConfig', JSON.stringify(config));

            const parsed = parseRerunConfig();

            expect(parsed).toEqual(config);
        });
    });

    describe('Invalid Config Handling', () => {
        test('should return null for invalid JSON', () => {
            sessionStorage.setItem('rerunConfig', 'not valid json {{{');

            const parsed = parseRerunConfig();

            expect(parsed).toBeNull();
            expect(sessionStorage.getItem('rerunConfig')).toBeNull();
        });

        test('should return null for empty string', () => {
            sessionStorage.setItem('rerunConfig', '');

            const parsed = parseRerunConfig();

            expect(parsed).toBeNull();
        });

        test('should return null when no config exists', () => {
            const parsed = parseRerunConfig();

            expect(parsed).toBeNull();
        });

        test('should handle truncated JSON', () => {
            sessionStorage.setItem('rerunConfig', '{"query": "test"');

            const parsed = parseRerunConfig();

            expect(parsed).toBeNull();
            expect(sessionStorage.getItem('rerunConfig')).toBeNull();
        });
    });

});
