#!/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);

        // 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}`);
                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);
