#!/usr/bin/env node
/**
 * Register CI test user via the real registration flow
 *
 * This script uses the same auth_helper.js that tests use,
 * ensuring we test the actual registration flow.
 *
 * Usage: node register_ci_user.js [base_url]
 */

const puppeteer = require('puppeteer');
const AuthHelper = require('./auth_helper.js');
const { getPuppeteerLaunchOptions } = require('./puppeteer_config');
const { CI_TEST_USER } = AuthHelper;

const BASE_URL = process.argv[2] || process.env.TEST_BASE_URL || 'http://127.0.0.1:5000';

async function main() {
    console.log(`Registering CI test user (${CI_TEST_USER.username}) at ${BASE_URL}...`);

    let browser;
    try {
        browser = await puppeteer.launch(getPuppeteerLaunchOptions());

        const page = await browser.newPage();
        const auth = new AuthHelper(page, BASE_URL);

        // Observe auth POST responses so the swallowed failure below can still
        // report the HTTP status. A login that fails because of a SQLCipher
        // KDF/cipher mismatch (DB-init env vs server env) returns the same
        // "Invalid username or password" 401 flash as a wrong password, so the
        // status plus the pointer below is the only CI-visible signal of cause.
        let lastAuthStatus = null;
        let lastAuthPath = null;
        page.on('response', (res) => {
            // This runs on Puppeteer's CDP event pump, OUTSIDE main()'s
            // try/catch — a throw here would escape as an unhandledRejection
            // and exit non-zero, defeating the script's exit-0-by-design
            // contract. Observation is best-effort, so guard it: diagnostics
            // must never fail the build.
            try {
                const req = res.request();
                if (
                    req.method() === 'POST' &&
                    (req.url().includes('/auth/login') || req.url().includes('/auth/register'))
                ) {
                    lastAuthStatus = res.status();
                    lastAuthPath = new URL(req.url()).pathname;
                }
            } catch {
                // best-effort observer — ignore any error
            }
        });

        // Try to register the CI test user
        try {
            await auth.register(CI_TEST_USER.username, CI_TEST_USER.password);
            console.log('Registration successful');
        } catch (regError) {
            // User might already exist, try to login to verify
            console.log(`Registration note: ${regError.message}`);
            console.log('Attempting login to verify user exists...');

            try {
                await auth.login(CI_TEST_USER.username, CI_TEST_USER.password);
                console.log('Login successful - user already exists');
            } catch (loginError) {
                console.log(`Login also failed: ${loginError.message}`);
                if (lastAuthStatus !== null) {
                    console.log(`  Last auth POST: ${lastAuthPath} -> HTTP ${lastAuthStatus}`);
                    if (lastAuthStatus === 401) {
                        // Self-gated: only jobs with a SEPARATE DB-init step
                        // (e.g. playwright-webkit) can hit a KDF mismatch. Jobs
                        // that create the user via this same server (e.g.
                        // responsive-ui) share one KDF env, so there a 401 is
                        // always just wrong creds / unregistered user — don't mislead.
                        console.log(
                            '  If this job pre-creates the user in a separate DB-init step, ' +
                            'a 401 can mean a SQLCipher KDF/cipher mismatch between the init ' +
                            'env and the server env (see the KDF note in ' +
                            '.github/workflows/playwright-webkit-tests.yml and PR #4775); ' +
                            'otherwise it means wrong credentials or an unregistered user.'
                        );
                    }
                }
                console.log('Warning: Could not register or login CI test user');
                console.log('Tests will fall back to creating their own users');
            }
        }

        console.log('CI test user setup complete');

    } catch (error) {
        console.error('Error during CI test user setup:', error.message);
        // Don't fail the workflow - tests have their own fallback
        console.log('Tests will fall back to creating their own users');
    } finally {
        if (browser) {
            await browser.close();
        }
    }
}

main().catch(console.error);
