/**
 * electron/driver.mjs — bundled Electron driver.
 *
 * Maps IDL verbs to the Electron MCP server's tool surface. The
 * transport.mcp_server_id="electron" string in manifest.json names the
 * MCP server config the resolver hands off to.
 *
 * Per ID-01 — the workflow prose NEVER names mcp__electron__* tools.
 * That mapping lives ONLY here in driver.mjs.
 *
 * IDL verb → MCP tool mapping (one place, one purpose):
 *   click          → mcp__electron__send_command_to_electron (button_click)
 *   type           → mcp__electron__send_command_to_electron (fill_input)
 *   key            → mcp__electron__send_command_to_electron (send_keyboard_shortcut)
 *   navigate       → mcp__electron__send_command_to_electron (navigate)
 *   screenshot     → mcp__electron__take_screenshot
 *   assert_visible → mcp__electron__send_command_to_electron (selector check)
 *   assert_text    → mcp__electron__send_command_to_electron (textContent match)
 *   wait_for_idle  → mcp__electron__send_command_to_electron (renderer-idle check)
 *   execute_js     → mcp__electron__send_command_to_electron (evaluate)
 *   dump_state     → mcp__electron__get_electron_window_info
 *
 * The driver is a thin adapter — same shape as browser/driver.mjs.
 */

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(`electron.${verb}: missing required args: ${check.missing.join(', ')}`)
	}
	if (verb === 'screenshot' && typeof args.output_path === 'string') {
		return ok([args.output_path])
	}
	if (verb === 'dump_state' && typeof args.output_path === 'string') {
		return ok([args.output_path])
	}
	return ok()
}

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

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

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

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

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

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

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

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

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

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