import assert from "node:assert/strict";
import { test } from "vitest";
import { isUsableRemoteQuestionResult } from "../../ask-user-questions.ts";
import {
	tryAutoResolveQuestions,
	tryHeadlessLocalAutoResolveQuestions,
} from "../../remote-questions/manager.ts";

test("remote question timeout auto-resolution picks the first option for each single-select question", () => {
	const answer = tryAutoResolveQuestions([
		{
			id: "lane",
			header: "Lane",
			question: "Which lane?",
			options: [
				{
					label: "Use the safer path (Recommended)",
					description: "Lower risk",
				},
				{ label: "Use the aggressive path", description: "Higher risk" },
			],
		},
		{
			id: "depth",
			header: "Depth",
			question: "How deep?",
			options: [
				{ label: "Standard (Recommended)", description: "Balanced" },
				{ label: "Thorough", description: "Deeper" },
			],
		},
	]);

	assert.deepEqual(answer, {
		answers: {
			lane: { answers: ["Use the safer path (Recommended)"] },
			depth: { answers: ["Standard (Recommended)"] },
		},
	});
});

test("remote question timeout auto-resolution refuses multi-select or empty prompts", () => {
	assert.equal(
		tryAutoResolveQuestions([
			{
				id: "multi",
				header: "Multi",
				question: "Pick many",
				allowMultiple: true,
				options: [
					{ label: "Alpha (Recommended)", description: "A" },
					{ label: "Beta", description: "B" },
				],
			},
		]),
		null,
	);

	assert.equal(
		tryAutoResolveQuestions([
			{
				id: "empty",
				header: "Empty",
				question: "Pick one",
				options: [],
			},
		]),
		null,
	);
});

test("ask_user_questions treats timeout auto-resolution as a usable remote answer", () => {
	assert.equal(
		isUsableRemoteQuestionResult({ timed_out: true, autoResolved: true }),
		true,
	);
	assert.equal(isUsableRemoteQuestionResult({ timed_out: true }), false);
	assert.equal(
		isUsableRemoteQuestionResult({ error: true, autoResolved: true }),
		false,
	);
	assert.equal(
		isUsableRemoteQuestionResult({ cancelled: true, autoResolved: true }),
		false,
	);
});

test("headless local auto-resolution fires after timeout when telegram is unavailable", async () => {
	let sleptMs = 0;
	const result = await tryHeadlessLocalAutoResolveQuestions(
		[
			{
				id: "depth_verification_M001_confirm",
				header: "Depth",
				question: "Is this sufficient?",
				options: [
					{ label: "Sufficient", description: "Proceed" },
					{ label: "Needs more", description: "Keep discussing" },
				],
			},
		],
		{
			hasUI: false,
			telegramUnavailable: true,
			unavailableReason: "no-telegram",
			policy: {
				channel: "telegram",
				timeoutMs: 5,
				autoResolveOnTimeout: true,
				autoResolveStrategy: "recommended-option",
			},
			sleepFn: async (ms) => {
				sleptMs = ms;
			},
		},
	);

	assert.equal(sleptMs, 5);
	assert.equal(result?.details?.autoResolved, true);
	assert.equal(result?.details?.localFallback, true);
	assert.deepEqual(result?.details?.response, {
		endInterview: false,
		answers: {
			depth_verification_M001_confirm: {
				selected: "Sufficient",
				notes: "",
			},
		},
	});
});

test("headless local auto-resolution does not run when telegram is available", async () => {
	let slept = false;
	const result = await tryHeadlessLocalAutoResolveQuestions(
		[
			{
				id: "depth_verification_M001_confirm",
				header: "Depth",
				question: "Is this sufficient?",
				options: [{ label: "Sufficient", description: "Proceed" }],
			},
		],
		{
			hasUI: false,
			telegramUnavailable: false,
			unavailableReason: "no-telegram",
			policy: {
				channel: "telegram",
				timeoutMs: 5,
				autoResolveOnTimeout: true,
				autoResolveStrategy: "recommended-option",
			},
			sleepFn: async () => {
				slept = true;
			},
		},
	);

	assert.equal(result, null);
	assert.equal(slept, false);
});

test("headless local auto-resolution does not run in interactive TUI mode", async () => {
	let slept = false;
	const result = await tryHeadlessLocalAutoResolveQuestions(
		[
			{
				id: "depth_verification_M001_confirm",
				header: "Depth",
				question: "Is this sufficient?",
				options: [{ label: "Sufficient", description: "Proceed" }],
			},
		],
		{
			hasUI: true,
			telegramUnavailable: true,
			unavailableReason: "no-telegram",
			policy: {
				channel: "telegram",
				timeoutMs: 5,
				autoResolveOnTimeout: true,
				autoResolveStrategy: "recommended-option",
			},
			sleepFn: async () => {
				slept = true;
			},
		},
	);

	assert.equal(result, null);
	assert.equal(slept, false);
});
