/**
 * PR-28 smoke — OpenTelemetry bootstrap contract.
 *
 * The SDK is third-party-vetted (no need to re-test its tracer
 * internals); we just verify:
 *   1. isOtelEnabled honours OTEL_TRACES_ENABLED
 *   2. startOtel no-ops when disabled (does NOT throw / hang)
 *   3. startOtel is idempotent (second call returns the same SDK)
 *
 * Live OTLP export to a real backend is the operator's manual test
 * (point at Honeycomb / Tempo / Datadog and watch traces flow in).
 *
 * Run: npx tsx scripts/smoke/pr28-otel.ts
 */
import './_lib';
import { ok, fail } from './_lib';

async function main(): Promise<void> {
    // Start with OTel disabled
    delete process.env.OTEL_TRACES_ENABLED;
    const { isOtelEnabled, startOtel } = await import('../../src/lib/observability/otel');
    if (isOtelEnabled()) fail('PR-28 default-off', 'OTel should default off');
    ok('OTel defaults to off when OTEL_TRACES_ENABLED is unset');

    // AUDIT-14: startOtel is now async — full SDK module graph is
    // dynamically imported only when OTEL_TRACES_ENABLED is true.
    const result = await startOtel();
    if (result !== undefined) fail('PR-28 noop', 'startOtel should return undefined when disabled');
    ok('startOtel no-ops when disabled');

    // Verify the env-flag toggles. Re-import is the same cached module
    // so we just exercise the helper directly.
    process.env.OTEL_TRACES_ENABLED = 'true';
    if (!isOtelEnabled()) fail('PR-28 flag', 'OTEL_TRACES_ENABLED=true should enable');
    ok('OTEL_TRACES_ENABLED=true flips isOtelEnabled to true');

    process.env.OTEL_TRACES_ENABLED = '1';
    if (!isOtelEnabled()) fail('PR-28 flag-1', 'OTEL_TRACES_ENABLED=1 should also enable');
    ok('OTEL_TRACES_ENABLED=1 also enables (numeric-string accepted)');

    process.env.OTEL_TRACES_ENABLED = 'false';
    if (isOtelEnabled()) fail('PR-28 flag-off', 'OTEL_TRACES_ENABLED=false should disable');
    ok('OTEL_TRACES_ENABLED=false disables');

    ok('PR-28 otel smoke', 'OTel bootstrap contract holds (disabled-by-default, env-flag-toggled)');
}

main().catch((e) => fail('PR-28 crashed', String(e)));
