/**
 * tui-walkthrough-slash.test.ts — 슬래시 명령어 전체 검증
 *
 * /help, /status, /models, /agents, /themes, /sessions, /new, /exit,
 * /restart, /connect, /mcps, /mcp-refresh, /variants, /logs,
 * /editor, /skills, /pointing, /timestamps, /thinking, /rename,
 * /timeline, /fork, /compact, /copy, /export, /share, /unshare,
 * /undo, /redo, /org, /quit, /q 등 전부.
 */

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 } from "./tui-walkthrough-driver"

void Log.init({ print: false })

describe("TUI walkthrough: slash commands", () => {
	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-slash-report.txt")
		await driver.kill()
		await disposeAllInstances()
		Flag.SAEEOL_EXPERIMENTAL_HTTPAPI = true
	})

	// ── 시스템 슬래시 명령 ──

	test("/help → help dialog", async () => {
		await driver.slash("help")
		driver.snapshot("slash-help")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/status → status dialog", async () => {
		await driver.slash("status")
		driver.snapshot("slash-status")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/themes → theme dialog", async () => {
		await driver.slash("themes")
		driver.snapshot("slash-themes")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/models → model dialog", async () => {
		await driver.slash("models")
		driver.snapshot("slash-models")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/agents → agent dialog", async () => {
		await driver.slash("agents")
		driver.snapshot("slash-agents")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/variants → variant dialog", async () => {
		await driver.slash("variants")
		driver.snapshot("slash-variants")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	// ── 세션 슬래시 명령 ──

	test("/sessions → session list", async () => {
		await driver.slash("sessions")
		driver.snapshot("slash-sessions")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/resume → session list alias", async () => {
		await driver.slash("resume")
		driver.snapshot("slash-resume")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/continue → session list alias", async () => {
		await driver.slash("continue")
		driver.snapshot("slash-continue")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/new → new session (home)", async () => {
		await driver.slash("new")
		driver.snapshot("slash-new")
		expect(driver.errors()).toHaveLength(0)
	})

	test("/clear → alias for /new", async () => {
		await driver.slash("clear")
		driver.snapshot("slash-clear")
		expect(driver.errors()).toHaveLength(0)
	})

	// ── 에이전트/MCP 슬래시 명령 ──

	test("/mcps → MCP dialog", async () => {
		await driver.slash("mcps")
		driver.snapshot("slash-mcps")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/mcp-refresh → refresh MCP servers", async () => {
		await driver.slash("mcp-refresh")
		driver.snapshot("slash-mcp-refresh")
		expect(driver.errors()).toHaveLength(0)
	})

	test("/connect → provider dialog", async () => {
		await driver.slash("connect")
		driver.snapshot("slash-connect")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	// ── 세션 관리 슬래시 명령 ──

	test("/timeline → timeline dialog", async () => {
		await driver.slash("timeline")
		driver.snapshot("slash-timeline")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/compact → compact session", async () => {
		await driver.slash("compact")
		driver.snapshot("slash-compact")
		expect(driver.errors()).toHaveLength(0)
	})

	test("/summarize → alias for compact", async () => {
		await driver.slash("summarize")
		driver.snapshot("slash-summarize")
		expect(driver.errors()).toHaveLength(0)
	})

	test("/rename → rename dialog", async () => {
		await driver.slash("rename")
		driver.snapshot("slash-rename")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/fork → fork dialog", async () => {
		await driver.slash("fork")
		driver.snapshot("slash-fork")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	// ── 편집/내보내기 ──

	test("/editor → open editor", async () => {
		await driver.slash("editor")
		driver.snapshot("slash-editor")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss(1000)
	})

	test("/skills → skills dialog", async () => {
		await driver.slash("skills")
		driver.snapshot("slash-skills")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/copy → copy transcript", async () => {
		await driver.slash("copy")
		driver.snapshot("slash-copy")
		expect(driver.errors()).toHaveLength(0)
	})

	test("/export → export dialog", async () => {
		await driver.slash("export")
		driver.snapshot("slash-export")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	// ── 토글 ──

	test("/timestamps → toggle timestamps", async () => {
		await driver.slash("timestamps")
		driver.snapshot("slash-timestamps")
		expect(driver.errors()).toHaveLength(0)
	})

	test("/toggle-timestamps → alias", async () => {
		await driver.slash("toggle-timestamps")
		driver.snapshot("slash-toggle-timestamps")
		expect(driver.errors()).toHaveLength(0)
	})

	test("/thinking → toggle thinking", async () => {
		await driver.slash("thinking")
		driver.snapshot("slash-thinking")
		expect(driver.errors()).toHaveLength(0)
	})

	test("/toggle-thinking → alias", async () => {
		await driver.slash("toggle-thinking")
		driver.snapshot("slash-toggle-thinking")
		expect(driver.errors()).toHaveLength(0)
	})

	// ── 포인팅 ──

	test("/pointing → pointing dialog", async () => {
		await driver.slash("pointing")
		driver.snapshot("slash-pointing")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/point → alias for pointing", async () => {
		await driver.slash("point")
		driver.snapshot("slash-point")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/unpointing → clear pointing", async () => {
		await driver.slash("unpointing")
		driver.snapshot("slash-unpointing")
		expect(driver.errors()).toHaveLength(0)
	})

	test("/unpoint → alias for unpointing", async () => {
		await driver.slash("unpoint")
		driver.snapshot("slash-unpoint")
		expect(driver.errors()).toHaveLength(0)
	})

	// ── 공유 (share/unshare는 provider 없이 에러 처리만) ──

	test("/share → share session", async () => {
		await driver.slash("share")
		driver.snapshot("slash-share")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	test("/unshare → unshare session", async () => {
		await driver.slash("unshare")
		driver.snapshot("slash-unshare")
		expect(driver.errors()).toHaveLength(0)
		await driver.dismiss()
	})

	// ── undo/redo ──

	test("/undo → undo message", async () => {
		await driver.slash("undo")
		driver.snapshot("slash-undo")
		expect(driver.errors()).toHaveLength(0)
	})

	test("/redo → redo message", async () => {
		await driver.slash("redo")
		driver.snapshot("slash-redo")
		expect(driver.errors()).toHaveLength(0)
	})

	// ── 로그 ──

	test("/logs → open log file", async () => {
		await driver.slash("logs")
		driver.snapshot("slash-logs")
		expect(driver.errors()).toHaveLength(0)
	})

	// ── 종료 (마지막) ──

	test("/quit → exit alias", async () => {
		await driver.slash("quit")
		await driver.wait(2000)
		driver.snapshot("slash-quit")
		expect(driver.errors()).toHaveLength(0)
	})
})
