/**
 * Shared mocks for /library/embedding-settings Playwright specs.
 *
 * Wires up `page.route()` interceptors for the rag/models, rag/settings,
 * and per-field /settings/api endpoints so the page can be exercised
 * without a real Ollama or Sentence-Transformers backend, and returns a
 * `state` object the calling test can read/mutate to simulate persistence
 * and assert what was actually saved.
 *
 * CommonJS to match the existing `mobile-utils.js` helper.
 */

const BASE_MODELS_PAYLOAD = {
    success: true,
    provider_options: [
        {
            value: 'sentence_transformers',
            label: 'Sentence Transformers (Local)',
            available: true,
        },
        { value: 'ollama', label: 'Ollama (Local)', available: true },
    ],
    providers: {
        sentence_transformers: [
            { value: 'all-MiniLM-L6-v2', label: 'all-MiniLM-L6-v2', is_embedding: true },
            { value: 'all-mpnet-base-v2', label: 'all-mpnet-base-v2', is_embedding: true },
            { value: 'multi-qa-MiniLM-L6-cos-v1', label: 'multi-qa-MiniLM-L6-cos-v1', is_embedding: true },
        ],
        ollama: [
            { value: 'nomic-embed-text', label: 'nomic-embed-text', is_embedding: true },
            { value: 'mxbai-embed-large', label: 'mxbai-embed-large', is_embedding: true },
            { value: 'snowflake-arctic-embed', label: 'snowflake-arctic-embed', is_embedding: true },
        ],
    },
};

const DEFAULT_TEXT_SEPARATORS = ['\n\n', '\n', '. ', ' ', ''];

function defaultSettings(overrides = {}) {
    return {
        embedding_provider: 'sentence_transformers',
        embedding_model: 'all-MiniLM-L6-v2',
        chunk_size: 1000,
        chunk_overlap: 200,
        splitter_type: 'recursive',
        distance_metric: 'cosine',
        index_type: 'flat',
        normalize_vectors: true,
        text_separators: [...DEFAULT_TEXT_SEPARATORS],
        ...overrides,
    };
}

/**
 * Wire up route mocks for the embedding-settings page. Returns a `state`
 * object the test can read/mutate to simulate persistence and assert what
 * was actually saved.
 *
 * Note on route order: Playwright matches routes in REVERSE registration
 * order (last-registered wins). The broad catch-all is registered first
 * so the specific endpoint handlers below override it where applicable.
 * Tests that need to override `/library/api/rag/models` (e.g. to add a
 * shared model across providers) should register their override AFTER
 * calling this function.
 */
async function mockEmbeddingApis(page, initialSettings) {
    const state = {
        settings: { ...initialSettings },
        ollamaUrl: 'http://localhost:11434',
        // Every PUT to /settings/api/local_search_embedding_model lands here
        // so the test can assert what was actually persisted.
        modelSaves: [],
        providerSaves: [],
        textSeparatorsSaves: [],
        // Counter incremented on each /library/api/rag/models GET. Tests use
        // this to wait deterministically for `loadAvailableModels()` to
        // finish — once the count increases past a baseline, any synthetic
        // `change` dispatch from `updateModelOptions()` has already fired.
        modelsFetches: 0,
    };

    await page.route('**/settings/api/**', async (route) => {
        await route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify({ success: true }),
        });
    });

    await page.route('**/library/api/rag/models', async (route) => {
        state.modelsFetches += 1;
        await route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify(BASE_MODELS_PAYLOAD),
        });
    });

    await page.route('**/library/api/rag/settings', async (route) => {
        await route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify({ success: true, settings: state.settings }),
        });
    });

    await page.route('**/settings/api/embeddings.ollama.url', async (route) => {
        const req = route.request();
        if (req.method() === 'GET') {
            await route.fulfill({
                status: 200,
                contentType: 'application/json',
                body: JSON.stringify({ value: state.ollamaUrl }),
            });
        } else {
            const body = req.postDataJSON() || {};
            state.ollamaUrl = body.value;
            await route.fulfill({
                status: 200,
                contentType: 'application/json',
                body: JSON.stringify({ success: true }),
            });
        }
    });

    await page.route('**/settings/api/local_search_embedding_model', async (route) => {
        const req = route.request();
        if (req.method() === 'GET') {
            await route.fulfill({
                status: 200,
                contentType: 'application/json',
                body: JSON.stringify({ value: state.settings.embedding_model }),
            });
            return;
        }
        const body = req.postDataJSON() || {};
        state.modelSaves.push(body.value);
        state.settings.embedding_model = body.value;
        await route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify({ success: true }),
        });
    });

    await page.route('**/settings/api/local_search_embedding_provider', async (route) => {
        const req = route.request();
        if (req.method() === 'GET') {
            await route.fulfill({
                status: 200,
                contentType: 'application/json',
                body: JSON.stringify({ value: state.settings.embedding_provider }),
            });
            return;
        }
        const body = req.postDataJSON() || {};
        state.providerSaves.push(body.value);
        state.settings.embedding_provider = body.value;
        await route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify({ success: true }),
        });
    });

    await page.route('**/settings/api/local_search_text_separators', async (route) => {
        const req = route.request();
        if (req.method() === 'GET') {
            await route.fulfill({
                status: 200,
                contentType: 'application/json',
                body: JSON.stringify({ value: state.settings.text_separators }),
            });
            return;
        }
        const body = req.postDataJSON() || {};
        state.textSeparatorsSaves.push(body.value);
        state.settings.text_separators = body.value;
        await route.fulfill({
            status: 200,
            contentType: 'application/json',
            body: JSON.stringify({ success: true }),
        });
    });

    return state;
}

module.exports = {
    BASE_MODELS_PAYLOAD,
    DEFAULT_TEXT_SEPARATORS,
    defaultSettings,
    mockEmbeddingApis,
};
