/**
 * tui-walkthrough-leader.test.ts — Leader 키 조합 전체 검증
 *
 * ctrl+x(leader) + 키 조합으로 실행되는 모든 기능을 한 번씩 누름.
 * 다이얼로그 열기/닫기, 토글, 액션 모두 포함.
 */

import { describe, expect, test, beforeAll, afterAll } from "bun:test"
import { tmpdir, disposeAllInstances } from "../fixture/fixture"
import * as Log from "@saeeol/core/util/log"
import { Flag } from "@saeeol/core/flag/flag"
import { TuiDriver, Key } from "./tui-walkthrough-driver"

void Log.init({ print: false })

describe("TUI walkthrough: leader keys", () => {
	let driver: TuiDriver
	let tmp: Awaited<ReturnType<typeof tmpdir>>

	beforeAll(async () => {
		tmp = await tmpdir({ git: true, config: {} })
		Flag.SAEEOL_EXPERIMENTAL_HTTPAPI = false
		driver = new TuiDriver(tmp.path)
		await driver.wait(6000)
	})

	afterAll(async () => {
		await driver.saveReport(".tui-leader-report.txt")
		await driver.kill()
		await disposeAllInstances()
		Flag.SAEEOL_EXPERIMENTAL_HTTPAPI = true
	})

	// ── 다이얼로그 열고 닫기 ──

	test("leader+l → session list dialog", async () => {
		await driver.leader("l")
		await driver.wait(1500)
		driver.snapshot("leader-l-session-list")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("leader+m → model list dialog", async () => {
		await driver.leader("m")
		await driver.wait(1500)
		driver.snapshot("leader-m-model-list")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("leader+a → agent list dialog", async () => {
		await driver.leader("a")
		await driver.wait(1500)
		driver.snapshot("leader-a-agent-list")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("leader+s → status dialog", async () => {
		await driver.leader("s")
		await driver.wait(1500)
		driver.snapshot("leader-s-status")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("leader+t → theme list dialog", async () => {
		await driver.leader("t")
		await driver.wait(1500)
		driver.snapshot("leader-t-theme-list")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("leader+g → session timeline dialog", async () => {
		await driver.leader("g")
		await driver.wait(1500)
		driver.snapshot("leader-g-timeline")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	// ── 토글 / 액션 ──

	test("leader+b → sidebar toggle (open)", async () => {
		await driver.leader("b")
		await driver.wait(800)
		driver.snapshot("leader-b-sidebar-open")
		expect(driver.errors()).toHaveLength(0)
	})

	test("leader+b → sidebar toggle (close)", async () => {
		await driver.leader("b")
		await driver.wait(800)
		driver.snapshot("leader-b-sidebar-close")
		expect(driver.errors()).toHaveLength(0)
	})

	test("leader+n → new session (home)", async () => {
		await driver.leader("n")
		await driver.wait(1000)
		driver.snapshot("leader-n-new-session")
		expect(driver.errors()).toHaveLength(0)
	})

	test("leader+c → session compact", async () => {
		await driver.leader("c")
		await driver.wait(1000)
		driver.snapshot("leader-c-compact")
		expect(driver.errors()).toHaveLength(0)
	})

	test("leader+h → toggle conceal / tips", async () => {
		await driver.leader("h")
		await driver.wait(800)
		driver.snapshot("leader-h-conceal-toggle")
		expect(driver.errors()).toHaveLength(0)
	})

	test("leader+= → feedback up", async () => {
		await driver.leader("=")
		await driver.wait(800)
		driver.snapshot("leader-eq-feedback-up")
		expect(driver.errors()).toHaveLength(0)
	})

	test("leader+- → feedback down", async () => {
		await driver.leader("-")
		await driver.wait(800)
		driver.snapshot("leader-minus-feedback-down")
		expect(driver.errors()).toHaveLength(0)
	})

	test("leader+y → copy message", async () => {
		await driver.leader("y")
		await driver.wait(800)
		driver.snapshot("leader-y-copy")
		expect(driver.errors()).toHaveLength(0)
	})

	test("leader+u → undo message", async () => {
		await driver.leader("u")
		await driver.wait(800)
		driver.snapshot("leader-u-undo")
		expect(driver.errors()).toHaveLength(0)
	})

	test("leader+r → redo message", async () => {
		await driver.leader("r")
		await driver.wait(800)
		driver.snapshot("leader-r-redo")
		expect(driver.errors()).toHaveLength(0)
	})

	// ── 세션 네비게이션 (leader+방향키) ──

	test("leader+down → child session first", async () => {
		driver.write(Key.ctrlX)
		await driver.wait(100)
		driver.write(Key.down)
		await driver.wait(800)
		driver.snapshot("leader-down-child-first")
		expect(driver.errors()).toHaveLength(0)
	})

	// ── leader+q → 종료 (마지막에) ──

	test("leader+q → exit", async () => {
		await driver.leader("q")
		await driver.wait(2000)
		driver.snapshot("leader-q-exit")
		expect(driver.errors()).toHaveLength(0)
	})
})
