import '../assert_dev_env.js';
import { build_test_backend_paths } from './build_test_backend_paths.js';
import { default_test_bootstrap_token, default_test_cookie_keys, default_test_keeper_password, default_test_keeper_username, } from './default_secrets.js';
/**
 * Capabilities shared by TS-family backends — same canonical
 * implementation, same feature set. No trusted-proxy phase (the test
 * binary doesn't enable proxy parsing) and no per-account login rate
 * limit (the TS canonical path leaves the limiter null in test mode).
 */
export const ts_default_capabilities = Object.freeze({
    bearer_auth: true,
    trusted_proxy: false,
    login_rate_limit: false,
    ws: true,
    sse: false,
    cell_crud: true,
    cell_relations: true,
    account_lifecycle: true,
});
/**
 * Capabilities for the Rust family. Adds `trusted_proxy: true` (the
 * Rust spine's client-IP middleware is always wired; the env-gate just
 * controls whether XFF is consulted vs the TCP peer IP) and
 * `login_rate_limit: true` (env-gated bucket on `/login` + `/password`).
 */
export const rust_default_capabilities = Object.freeze({
    bearer_auth: true,
    trusted_proxy: true,
    login_rate_limit: true,
    ws: true,
    sse: false,
    cell_crud: true,
    cell_relations: true,
    account_lifecycle: true,
});
/** Bootstrap block built from the default secrets + supplied paths. */
const build_default_bootstrap = (paths, overrides) => ({
    token_path: paths.bootstrap_token_path,
    token: default_test_bootstrap_token,
    username: default_test_keeper_username,
    password: default_test_keeper_password,
    daemon_token_path: paths.daemon_token_path,
    ...overrides,
});
/**
 * Shared builder for TS-family backends (Deno + Node). Owns the common
 * env baseline (NODE_ENV, HOST, PORT, in-memory PGlite, cookie keys,
 * bootstrap token path) so per-backend factories only declare what
 * genuinely differs.
 */
export const make_default_ts_backend_config = (opts) => {
    const { name, port, start_command, database_url = 'memory://', extra_env, capabilities = ts_default_capabilities, paths = build_test_backend_paths(name), bootstrap_overrides, port_env_var = 'PORT', cookie_name = 'fuz_session', } = opts;
    return {
        name,
        start_command,
        base_url: `http://localhost:${port}`,
        rpc_path: '/api/rpc',
        ws_path: '/api/ws',
        health_path: '/health',
        bootstrap_path: '/api/account/bootstrap',
        cookie_name,
        startup_timeout_ms: 30_000,
        env: {
            NODE_ENV: 'development',
            HOST: 'localhost',
            [port_env_var]: String(port),
            DATABASE_URL: database_url,
            SECRET_FUZ_COOKIE_KEYS: default_test_cookie_keys,
            FUZ_ALLOWED_ORIGINS: 'http://localhost:*',
            FUZ_BOOTSTRAP_TOKEN_PATH: paths.bootstrap_token_path,
            ...extra_env,
        },
        bootstrap: build_default_bootstrap(paths, bootstrap_overrides),
        capabilities,
    };
};
/**
 * Shared builder for Rust-family backends. Owns the common env baseline
 * (RUST_LOG, HOST, port, real Postgres, cookie keys, bootstrap token
 * path, the `FUZ_TESTING_RESET_DB_ON_STARTUP=true` self-wipe gate) plus
 * the 120s startup window for cargo's first-run build cost.
 */
export const make_default_rust_backend_config = (opts) => {
    const { name, port, start_command, database_url, extra_env, capabilities = rust_default_capabilities, paths = build_test_backend_paths(name), bootstrap_overrides, port_env_var = 'PORT', rust_log = 'info', cookie_name = 'fuz_session', } = opts;
    return {
        name,
        start_command,
        base_url: `http://localhost:${port}`,
        rpc_path: '/api/rpc',
        ws_path: '/api/ws',
        health_path: '/health',
        bootstrap_path: '/api/account/bootstrap',
        cookie_name,
        startup_timeout_ms: 120_000,
        env: {
            RUST_LOG: rust_log,
            HOST: 'localhost',
            [port_env_var]: String(port),
            DATABASE_URL: database_url,
            SECRET_FUZ_COOKIE_KEYS: default_test_cookie_keys,
            FUZ_ALLOWED_ORIGINS: 'http://localhost:*',
            FUZ_BOOTSTRAP_TOKEN_PATH: paths.bootstrap_token_path,
            // Self-wipe the auth-namespace schema before migrations on every
            // boot. Read by `fuz_testing::reset_db_on_startup_if_env_set`,
            // which the consumer's test binary invokes from its
            // `pre_migration_hook` between pool creation and
            // `fuz_db::run_migrations`. The `_testing_reset` RPC action
            // (per-test reset) is orthogonal.
            FUZ_TESTING_RESET_DB_ON_STARTUP: 'true',
            ...extra_env,
        },
        bootstrap: build_default_bootstrap(paths, bootstrap_overrides),
        capabilities,
    };
};
