package inbox

import (
	"os"
	"path/filepath"
	"testing"
	"time"
)

func TestInboxLifecycle(t *testing.T) {
	t.Parallel()

	dbPath := filepath.Join(t.TempDir(), "coord.db")

	initOut := runInboxCommand(t, "--db", dbPath, "--json", "init")
	var initResp map[string]any
	mustDecodeJSON(t, initOut, &initResp)
	if initResp["ok"] != true {
		t.Fatalf("expected init ok=true, got %#v", initResp)
	}

	sendOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"send",
		"--from", "leader",
		"--to", "worker-a",
		"--subject", "Implement feature X",
		"--summary", "Add retry policy",
		"--body", "Implement retry handling for the HTTP client.",
		"--run", "run_blog_001",
		"--task", "T1",
	)

	var sendResp map[string]any
	mustDecodeJSON(t, sendOut, &sendResp)
	threadID := nestedString(t, sendResp, "data", "thread", "thread_id")
	threadStatus := nestedString(t, sendResp, "data", "thread", "status")
	if threadStatus != "pending" {
		t.Fatalf("expected pending thread, got %q", threadStatus)
	}

	fetchOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"fetch",
		"--agent", "worker-a",
		"--status", "pending",
	)

	var fetchResp map[string]any
	mustDecodeJSON(t, fetchOut, &fetchResp)
	threadsValue := nestedValue(t, fetchResp, "data", "threads")
	threads, ok := threadsValue.([]any)
	if !ok || len(threads) != 1 {
		t.Fatalf("expected one fetched thread, got %#v", threadsValue)
	}

	claimOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"claim",
		"--agent", "worker-a",
		"--thread", threadID,
		"--lease-seconds", "300",
	)

	var claimResp map[string]any
	mustDecodeJSON(t, claimOut, &claimResp)
	claimedStatus := nestedString(t, claimResp, "data", "thread", "status")
	if claimedStatus != "claimed" {
		t.Fatalf("expected claimed thread, got %q", claimedStatus)
	}

	updateOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"update",
		"--agent", "worker-a",
		"--thread", threadID,
		"--status", "in_progress",
		"--summary", "Implementation started",
		"--body", "Scanning current HTTP client usage.",
	)

	var updateResp map[string]any
	mustDecodeJSON(t, updateOut, &updateResp)
	updatedStatus := nestedString(t, updateResp, "data", "thread", "status")
	if updatedStatus != "in_progress" {
		t.Fatalf("expected in_progress thread, got %q", updatedStatus)
	}

	blockedOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"update",
		"--agent", "worker-a",
		"--thread", threadID,
		"--status", "blocked",
		"--summary", "Need timeout decision",
		"--payload-json", `{"question":"Should retries apply to read timeouts?"}`,
	)

	var blockedResp map[string]any
	mustDecodeJSON(t, blockedOut, &blockedResp)
	blockedStatus := nestedString(t, blockedResp, "data", "thread", "status")
	if blockedStatus != "blocked" {
		t.Fatalf("expected blocked thread, got %q", blockedStatus)
	}

	replyOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"reply",
		"--from", "leader",
		"--to", "worker-a",
		"--thread", threadID,
		"--summary", "Retry read timeouts",
		"--body", "Yes, include read timeouts in the retry policy.",
	)

	var replyResp map[string]any
	mustDecodeJSON(t, replyOut, &replyResp)
	replyKind := nestedString(t, replyResp, "data", "message", "kind")
	if replyKind != "answer" {
		t.Fatalf("expected answer reply, got %q", replyKind)
	}

	doneOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"done",
		"--agent", "worker-a",
		"--thread", threadID,
		"--summary", "Retry policy implemented",
		"--body", "The HTTP client now retries the selected transient failures.",
	)

	var doneResp map[string]any
	mustDecodeJSON(t, doneOut, &doneResp)
	doneStatus := nestedString(t, doneResp, "data", "thread", "status")
	if doneStatus != "done" {
		t.Fatalf("expected done thread, got %q", doneStatus)
	}

	showOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"show",
		"--thread", threadID,
	)

	var showResp map[string]any
	mustDecodeJSON(t, showOut, &showResp)
	showStatus := nestedString(t, showResp, "data", "thread", "status")
	if showStatus != "done" {
		t.Fatalf("expected show status done, got %q", showStatus)
	}
	messagesValue := nestedValue(t, showResp, "data", "messages")
	messages, ok := messagesValue.([]any)
	if !ok || len(messages) != 6 {
		t.Fatalf("expected six messages in thread history, got %#v", messagesValue)
	}
}

func TestInboxFailLifecycle(t *testing.T) {
	t.Parallel()

	dbPath := filepath.Join(t.TempDir(), "coord.db")

	runInboxCommand(t, "--db", dbPath, "--json", "init")

	sendOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"send",
		"--from", "leader",
		"--to", "worker-b",
		"--subject", "Investigate failing migration",
		"--summary", "Check migration failure",
		"--run", "run_blog_002",
		"--task", "T2",
	)

	var sendResp map[string]any
	mustDecodeJSON(t, sendOut, &sendResp)
	threadID := nestedString(t, sendResp, "data", "thread", "thread_id")

	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"claim",
		"--agent", "worker-b",
		"--thread", threadID,
	)

	failOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"fail",
		"--agent", "worker-b",
		"--thread", threadID,
		"--summary", "Migration failed",
		"--body", "The migration cannot proceed because the prior schema is inconsistent.",
	)

	var failResp map[string]any
	mustDecodeJSON(t, failOut, &failResp)
	failStatus := nestedString(t, failResp, "data", "thread", "status")
	if failStatus != "failed" {
		t.Fatalf("expected failed thread, got %q", failStatus)
	}

	showOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"show",
		"--thread", threadID,
	)

	var showResp map[string]any
	mustDecodeJSON(t, showOut, &showResp)
	showStatus := nestedString(t, showResp, "data", "thread", "status")
	if showStatus != "failed" {
		t.Fatalf("expected show status failed, got %q", showStatus)
	}
}

func TestInboxRenewWaitReplyAndCancel(t *testing.T) {
	t.Parallel()

	dbPath := filepath.Join(t.TempDir(), "coord.db")

	runInboxCommand(t, "--db", dbPath, "--json", "init")

	sendOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"send",
		"--from", "leader",
		"--to", "worker-c",
		"--subject", "Investigate auth edge case",
		"--summary", "Check auth redirect behavior",
		"--run", "run_blog_003",
		"--task", "T3",
	)

	var sendResp map[string]any
	mustDecodeJSON(t, sendOut, &sendResp)
	threadID := nestedString(t, sendResp, "data", "thread", "thread_id")

	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"claim",
		"--agent", "worker-c",
		"--thread", threadID,
		"--lease-seconds", "300",
	)

	renewOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"renew",
		"--agent", "worker-c",
		"--thread", threadID,
		"--lease-seconds", "600",
	)

	var renewResp map[string]any
	mustDecodeJSON(t, renewOut, &renewResp)
	if got := nestedString(t, renewResp, "data", "message", "summary"); got != "lease renewed" {
		t.Fatalf("expected lease renewed summary, got %q", got)
	}

	blockedOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"update",
		"--agent", "worker-c",
		"--thread", threadID,
		"--status", "blocked",
		"--summary", "Need policy decision",
		"--body", "Should guest users be redirected to login or shown a 403 page?",
	)

	var blockedResp map[string]any
	mustDecodeJSON(t, blockedOut, &blockedResp)
	blockedMessageID := nestedString(t, blockedResp, "data", "message", "message_id")

	type commandResult struct {
		stdout string
		stderr string
		exit   int
	}

	waitCh := make(chan commandResult, 1)
	go func() {
		stdout, stderr, exitCode := executeInboxCommand(
			"--db", dbPath,
			"--agent", "worker-c",
			"--json",
			"wait-reply",
			"--thread", threadID,
			"--after-message", blockedMessageID,
			"--timeout-seconds", "2",
		)
		waitCh <- commandResult{stdout: stdout, stderr: stderr, exit: exitCode}
	}()

	time.Sleep(200 * time.Millisecond)

	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"reply",
		"--from", "leader",
		"--to", "worker-c",
		"--thread", threadID,
		"--summary", "Redirect to login",
		"--body", "Redirect guests to login for the MVP.",
	)

	var waitResult commandResult
	select {
	case waitResult = <-waitCh:
	case <-time.After(3 * time.Second):
		t.Fatal("wait-reply command did not return")
	}

	if waitResult.exit != 0 {
		t.Fatalf("wait-reply failed with exit=%d\nstderr:\n%s\nstdout:\n%s", waitResult.exit, waitResult.stderr, waitResult.stdout)
	}

	var waitResp map[string]any
	mustDecodeJSON(t, waitResult.stdout, &waitResp)
	if woke, ok := nestedValue(t, waitResp, "data", "woke").(bool); !ok || !woke {
		t.Fatalf("expected wait-reply to wake, got %#v", nestedValue(t, waitResp, "data", "woke"))
	}
	if kind := nestedString(t, waitResp, "data", "message", "kind"); kind != "answer" {
		t.Fatalf("expected answer wake message, got %q", kind)
	}

	stdout, _, exitCode := executeInboxCommand(
		"--db", dbPath,
		"--agent", "worker-c",
		"--json",
		"fetch",
		"--status", "blocked",
		"--unread",
	)
	if exitCode != 10 {
		t.Fatalf("expected blocked unread list to be cleared after wait-reply, got exit %d with %s", exitCode, stdout)
	}

	cancelOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"cancel",
		"--agent", "leader",
		"--thread", threadID,
		"--reason", "Task superseded by a larger refactor",
	)

	var cancelResp map[string]any
	mustDecodeJSON(t, cancelOut, &cancelResp)
	if status := nestedString(t, cancelResp, "data", "thread", "status"); status != "cancelled" {
		t.Fatalf("expected cancelled thread, got %q", status)
	}
}

func TestInboxWatchListUnreadAndAppend(t *testing.T) {
	t.Parallel()

	tempDir := t.TempDir()
	dbPath := filepath.Join(tempDir, "coord.db")
	bodyPath := filepath.Join(tempDir, "task.md")

	if err := os.WriteFile(bodyPath, []byte("Implement the initial admin post editor."), 0o644); err != nil {
		t.Fatalf("write body file: %v", err)
	}

	runInboxCommand(t, "--db", dbPath, "--json", "init")

	type commandResult struct {
		stdout string
		stderr string
		exit   int
	}

	watchCh := make(chan commandResult, 1)
	go func() {
		stdout, stderr, exitCode := executeInboxCommand(
			"--db", dbPath,
			"--json",
			"watch",
			"--agent", "worker-d",
			"--status", "pending",
			"--timeout-seconds", "2",
		)
		watchCh <- commandResult{stdout: stdout, stderr: stderr, exit: exitCode}
	}()

	time.Sleep(200 * time.Millisecond)

	sendOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"send",
		"--from", "leader",
		"--to", "worker-d",
		"--subject", "Build admin editor",
		"--summary", "Create the first editor screen",
		"--body-file", bodyPath,
		"--artifact", bodyPath,
		"--artifact-kind", "brief",
		"--artifact-metadata-json", `{"label":"task-brief"}`,
		"--run", "run_blog_004",
		"--task", "T4",
	)

	var sendResp map[string]any
	mustDecodeJSON(t, sendOut, &sendResp)
	threadID := nestedString(t, sendResp, "data", "thread", "thread_id")

	var watchResult commandResult
	select {
	case watchResult = <-watchCh:
	case <-time.After(3 * time.Second):
		t.Fatal("watch command did not return")
	}

	if watchResult.exit != 0 {
		t.Fatalf("watch failed with exit=%d\nstderr:\n%s\nstdout:\n%s", watchResult.exit, watchResult.stderr, watchResult.stdout)
	}

	var watchResp map[string]any
	mustDecodeJSON(t, watchResult.stdout, &watchResp)
	if woke, ok := nestedValue(t, watchResp, "data", "woke").(bool); !ok || !woke {
		t.Fatalf("expected watch to wake, got %#v", nestedValue(t, watchResp, "data", "woke"))
	}
	if watchedThreadID := nestedString(t, watchResp, "data", "thread", "thread_id"); watchedThreadID != threadID {
		t.Fatalf("expected watch on thread %s, got %s", threadID, watchedThreadID)
	}

	fetchOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"fetch",
		"--agent", "worker-d",
		"--status", "pending",
		"--unread",
	)

	var fetchResp map[string]any
	mustDecodeJSON(t, fetchOut, &fetchResp)
	fetchedThreads, ok := nestedValue(t, fetchResp, "data", "threads").([]any)
	if !ok || len(fetchedThreads) != 1 {
		t.Fatalf("expected one unread pending thread, got %#v", nestedValue(t, fetchResp, "data", "threads"))
	}

	listOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"list",
		"--assigned-to", "worker-d",
		"--status", "pending",
	)

	var listResp map[string]any
	mustDecodeJSON(t, listOut, &listResp)
	listedThreads, ok := nestedValue(t, listResp, "data", "threads").([]any)
	if !ok || len(listedThreads) != 1 {
		t.Fatalf("expected one listed thread, got %#v", nestedValue(t, listResp, "data", "threads"))
	}

	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"send",
		"--from", "leader",
		"--to", "worker-d",
		"--thread", threadID,
		"--summary", "Use a markdown editor",
		"--body", "Prefer a textarea-based markdown editor for v1.",
	)

	showOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"show",
		"--thread", threadID,
	)

	var showResp map[string]any
	mustDecodeJSON(t, showOut, &showResp)
	messages, ok := nestedValue(t, showResp, "data", "messages").([]any)
	if !ok || len(messages) != 2 {
		t.Fatalf("expected two messages after append, got %#v", nestedValue(t, showResp, "data", "messages"))
	}
	firstMessage, ok := messages[0].(map[string]any)
	if !ok {
		t.Fatalf("expected first message object, got %#v", messages[0])
	}
	if firstMessage["body"] != "Implement the initial admin post editor." {
		t.Fatalf("expected body-file content in first message, got %#v", firstMessage["body"])
	}
	artifacts, ok := firstMessage["artifacts"].([]any)
	if !ok || len(artifacts) != 1 {
		t.Fatalf("expected one artifact on first message, got %#v", firstMessage["artifacts"])
	}
	firstArtifact, ok := artifacts[0].(map[string]any)
	if !ok {
		t.Fatalf("expected artifact object, got %#v", artifacts[0])
	}
	if firstArtifact["path"] != bodyPath {
		t.Fatalf("expected artifact path %q, got %#v", bodyPath, firstArtifact["path"])
	}
	if firstArtifact["kind"] != "brief" {
		t.Fatalf("expected artifact kind brief, got %#v", firstArtifact["kind"])
	}
}

func TestInboxUnreadReadCursor(t *testing.T) {
	t.Parallel()

	dbPath := filepath.Join(t.TempDir(), "coord.db")

	runInboxCommand(t, "--db", dbPath, "--json", "init")

	sendOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"send",
		"--from", "leader",
		"--to", "worker-e",
		"--subject", "Review navbar copy",
		"--summary", "Check top nav wording",
	)

	var sendResp map[string]any
	mustDecodeJSON(t, sendOut, &sendResp)
	threadID := nestedString(t, sendResp, "data", "thread", "thread_id")

	fetchOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"fetch",
		"--agent", "worker-e",
		"--status", "pending",
		"--unread",
	)

	var fetchResp map[string]any
	mustDecodeJSON(t, fetchOut, &fetchResp)
	threads, ok := nestedValue(t, fetchResp, "data", "threads").([]any)
	if !ok || len(threads) != 1 {
		t.Fatalf("expected one unread pending thread, got %#v", nestedValue(t, fetchResp, "data", "threads"))
	}

	runInboxCommand(
		t,
		"--db", dbPath,
		"--agent", "worker-e",
		"--json",
		"show",
		"--thread", threadID,
		"--mark-read",
	)

	stdout, _, exitCode := executeInboxCommand(
		"--db", dbPath,
		"--json",
		"fetch",
		"--agent", "worker-e",
		"--status", "pending",
		"--unread",
	)
	if exitCode != 10 {
		t.Fatalf("expected unread fetch to clear after mark-read, got exit %d with %s", exitCode, stdout)
	}

	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"send",
		"--from", "leader",
		"--to", "worker-e",
		"--thread", threadID,
		"--summary", "Use sentence case",
		"--body", "Keep the nav labels in sentence case.",
	)

	fetchOut = runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"fetch",
		"--agent", "worker-e",
		"--status", "pending",
		"--unread",
	)

	mustDecodeJSON(t, fetchOut, &fetchResp)
	threads, ok = nestedValue(t, fetchResp, "data", "threads").([]any)
	if !ok || len(threads) != 1 {
		t.Fatalf("expected unread thread to reappear after new message, got %#v", nestedValue(t, fetchResp, "data", "threads"))
	}
}

func TestInboxJSONErrorsAndExitCodes(t *testing.T) {
	t.Parallel()

	dbPath := filepath.Join(t.TempDir(), "coord.db")

	if _, _, exitCode := executeInboxCommand("--db", dbPath, "--json", "init"); exitCode != 0 {
		t.Fatalf("expected init exit code 0, got %d", exitCode)
	}

	stdout, _, exitCode := executeInboxCommand(
		"--db", dbPath,
		"--json",
		"fetch",
		"--agent", "worker-z",
		"--status", "pending",
	)
	if exitCode != 10 {
		t.Fatalf("expected fetch no-match exit code 10, got %d", exitCode)
	}
	assertErrorJSON(t, stdout, "no_matching_work")

	stdout, _, exitCode = executeInboxCommand(
		"--db", dbPath,
		"--json",
		"claim",
		"--agent", "worker-z",
		"--thread", "thr_missing",
	)
	if exitCode != 40 {
		t.Fatalf("expected claim missing-thread exit code 40, got %d", exitCode)
	}
	assertErrorJSON(t, stdout, "not_found")

	sendOut := runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"send",
		"--from", "leader",
		"--to", "worker-z",
		"--subject", "Review cache settings",
		"--summary", "Check cache config",
	)

	var sendResp map[string]any
	mustDecodeJSON(t, sendOut, &sendResp)
	threadID := nestedString(t, sendResp, "data", "thread", "thread_id")

	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"claim",
		"--agent", "worker-z",
		"--thread", threadID,
	)

	stdout, _, exitCode = executeInboxCommand(
		"--db", dbPath,
		"--json",
		"claim",
		"--agent", "worker-y",
		"--thread", threadID,
	)
	if exitCode != 20 {
		t.Fatalf("expected lease conflict exit code 20, got %d", exitCode)
	}
	assertErrorJSON(t, stdout, "lease_conflict")

	stdout, _, exitCode = executeInboxCommand(
		"--db", dbPath,
		"--json",
		"send",
		"--from", "leader",
		"--to", "worker-z",
		"--subject", "Invalid payload json",
		"--payload-json", "not-json",
	)
	if exitCode != 30 {
		t.Fatalf("expected invalid input exit code 30, got %d", exitCode)
	}
	assertErrorJSON(t, stdout, "invalid_input")

	stdout, _, exitCode = executeInboxCommand(
		"--db", dbPath,
		"--json",
		"send",
		"--from", "leader",
		"--to", "worker-z",
		"--subject", "Invalid artifact json",
		"--artifact", "/tmp/report.md",
		"--artifact-metadata-json", "not-json",
	)
	if exitCode != 30 {
		t.Fatalf("expected invalid artifact metadata exit code 30, got %d", exitCode)
	}
	assertErrorJSON(t, stdout, "invalid_input")
}
