import '../assert_dev_env.js';
import { build_test_backend_paths } from './build_test_backend_paths.js';
import { make_default_rust_backend_config, rust_default_capabilities, } from './default_backend_configs.js';
/** Env var naming the prebuilt `testing_spine_stub` binary. Required when `binary_path` is omitted. */
export const SPINE_STUB_BIN_ENV = 'FUZ_TESTING_SPINE_STUB_BIN';
/** Default listening port — slots beside zzz's 1175/1176; matches the binary's `DEFAULT_PORT`. */
export const SPINE_STUB_DEFAULT_PORT = 1177;
/** Default Postgres database — real PG (PGlite isn't reachable from `tokio-postgres`). */
export const SPINE_STUB_DEFAULT_DATABASE_URL = 'postgres://localhost/fuz_app_test_spine_stub';
/**
 * Build the `BackendConfig` for `testing_spine_stub`. Resolves the binary
 * from `options.binary_path` or `FUZ_TESTING_SPINE_STUB_BIN`; throws when
 * neither is set so a missing build surfaces as a clear error rather than
 * a confusing spawn failure. Reconciles the binary's env contract: port
 * via `--port` (and `FUZ_SPINE_STUB_PORT`), daemon-token dir via
 * `FUZ_SPINE_STUB_DIR` (anchored to `paths.root` so the written
 * `{dir}/run/daemon_token` matches the path `spawn_backend` reads).
 *
 * @throws Error when no binary path is available.
 */
export const spine_stub_backend_config = (options = {}) => {
    const { port = SPINE_STUB_DEFAULT_PORT, database_url = SPINE_STUB_DEFAULT_DATABASE_URL, binary_path = process.env[SPINE_STUB_BIN_ENV], } = options;
    if (!binary_path) {
        throw new Error(`spine_stub_backend_config: no binary path — set ${SPINE_STUB_BIN_ENV} to a prebuilt ` +
            '`testing_spine_stub` binary (build it with `cargo build -p testing_spine_stub --release`) ' +
            'or pass `binary_path`.');
    }
    const name = 'spine_stub';
    const paths = build_test_backend_paths(name);
    return make_default_rust_backend_config({
        name,
        port,
        // `--port` is the binary's authoritative port input; the
        // `FUZ_SPINE_STUB_PORT` env the builder also sets (via `port_env_var`)
        // is the lower-precedence fallback — both carry the same value.
        start_command: [binary_path, '--port', String(port)],
        database_url,
        // The stub now serves `GET /api/admin/audit/stream` (the spine
        // `fuz_realtime::SseRegistry` + audit listener), so it advertises `sse`
        // like the TS spines — the cross-process SSE suite's three cases run.
        capabilities: { ...rust_default_capabilities, sse: true },
        port_env_var: 'FUZ_SPINE_STUB_PORT',
        rust_log: 'info,testing_spine_stub=info',
        paths,
        extra_env: {
            // The binary writes its daemon-token JSON to
            // `{FUZ_SPINE_STUB_DIR}/run/daemon_token`; anchoring the dir to
            // `paths.root` makes that equal `paths.daemon_token_path`, which
            // `spawn_backend` reads after the health probe.
            FUZ_SPINE_STUB_DIR: paths.root,
        },
    });
};
