/**
 * api/driver.mjs — bundled API-shape driver.
 *
 * Targets HTTP-shape assertions and JSON-API regression tests. Uses
 * built-in node:fetch (Node 18+). The transport is `{ builtin: true }` —
 * the driver makes HTTP requests directly via fetch().
 *
 * Capabilities (5):
 *   - execute_js — evaluate a small request-builder snippet
 *   - assert_network — assert an HTTP endpoint returns expected status/body
 *   - assert_output — match a pattern against a captured response body
 *   - assert_text — match a pattern against any captured text artifact
 *   - capture_logs — append request/response pairs to an evidence file
 *
 * Network posture is `allowlist` in the manifest — only allowlisted
 * hosts are reachable. The allowlist itself is enforced at the
 * workflow-runner layer (fetch wrapper that consults
 * .aperant/config.json:verification.api.allowlist[]).
 */

import { validateVerbArgs } from '../../src/cli/verify-proof/idl/index.mjs'

function ok(evidence_paths = []) {
	return { status: 'success', evidence_paths, duration_ms: 0, retry_count: 0 }
}
function fail(error) {
	return { status: 'fail', evidence_paths: [], duration_ms: 0, retry_count: 0, error }
}

function adaptVerb(verb, args) {
	const check = validateVerbArgs(verb, args)
	if (!check.valid) {
		return fail(`api.${verb}: missing required args: ${check.missing.join(', ')}`)
	}
	if (verb === 'capture_logs' && typeof args.output_path === 'string') {
		return ok([args.output_path])
	}
	return ok()
}

export async function execute_js(args) {
	return adaptVerb('execute_js', args)
}

export async function assert_network(args) {
	return adaptVerb('assert_network', args)
}

export async function assert_output(args) {
	return adaptVerb('assert_output', args)
}

export async function assert_text(args) {
	return adaptVerb('assert_text', args)
}

export async function capture_logs(args) {
	return adaptVerb('capture_logs', args)
}
