package orch

import (
	"path/filepath"
	"testing"
)

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

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

	runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"run", "init",
		"--run", "run_blog_001",
		"--goal", "Build blog MVP",
		"--summary", "Public blog plus admin CRUD",
	)

	taskOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"task", "add",
		"--run", "run_blog_001",
		"--task", "T1",
		"--title", "Implement retry policy",
		"--summary", "Add retry policy to HTTP client",
		"--default-to", "worker-a",
	)

	var taskResp map[string]any
	mustDecodeJSON(t, taskOut, &taskResp)
	if got := nestedString(t, taskResp, "data", "task", "status"); got != "ready" {
		t.Fatalf("expected new task to become ready, got %q", got)
	}

	readyOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"ready",
		"--run", "run_blog_001",
	)

	var readyResp map[string]any
	mustDecodeJSON(t, readyOut, &readyResp)
	readyTasks := nestedArray(t, readyResp, "data", "tasks")
	if len(readyTasks) != 1 {
		t.Fatalf("expected one ready task, got %#v", readyTasks)
	}
	readyTask, ok := readyTasks[0].(map[string]any)
	if !ok {
		t.Fatalf("expected ready task object, got %#v", readyTasks[0])
	}
	if taskID, _ := readyTask["task_id"].(string); taskID != "T1" {
		t.Fatalf("expected ready task T1, got %#v", readyTask["task_id"])
	}

	dispatchOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"dispatch",
		"--run", "run_blog_001",
		"--task", "T1",
		"--body", "Implement retry handling for the HTTP client.",
	)

	var dispatchResp map[string]any
	mustDecodeJSON(t, dispatchOut, &dispatchResp)
	if got := nestedString(t, dispatchResp, "data", "task", "status"); got != "dispatched" {
		t.Fatalf("expected dispatched task, got %q", got)
	}
	threadID := nestedString(t, dispatchResp, "data", "attempt", "thread_id")

	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"claim",
		"--agent", "worker-a",
		"--thread", threadID,
	)
	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"update",
		"--agent", "worker-a",
		"--thread", threadID,
		"--status", "in_progress",
		"--summary", "Implementation started",
	)

	reconcileOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"reconcile",
		"--run", "run_blog_001",
	)

	var reconcileResp map[string]any
	mustDecodeJSON(t, reconcileOut, &reconcileResp)
	updatedTasks := nestedArray(t, reconcileResp, "data", "updated_tasks")
	if len(updatedTasks) != 1 {
		t.Fatalf("expected one updated task after running reconcile, got %#v", updatedTasks)
	}
	runningTask, ok := updatedTasks[0].(map[string]any)
	if !ok {
		t.Fatalf("expected updated task object, got %#v", updatedTasks[0])
	}
	if status, _ := runningTask["status"].(string); status != "running" {
		t.Fatalf("expected running task after reconcile, got %#v", runningTask["status"])
	}

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

	reconcileDoneOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"reconcile",
		"--run", "run_blog_001",
	)

	var reconcileDoneResp map[string]any
	mustDecodeJSON(t, reconcileDoneOut, &reconcileDoneResp)
	updatedTasks = nestedArray(t, reconcileDoneResp, "data", "updated_tasks")
	if len(updatedTasks) != 1 {
		t.Fatalf("expected one updated task after done reconcile, got %#v", updatedTasks)
	}
	doneTask, ok := updatedTasks[0].(map[string]any)
	if !ok {
		t.Fatalf("expected updated task object, got %#v", updatedTasks[0])
	}
	if status, _ := doneTask["status"].(string); status != "done" {
		t.Fatalf("expected done task after reconcile, got %#v", doneTask["status"])
	}

	statusOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"status",
		"--run", "run_blog_001",
	)

	var statusResp map[string]any
	mustDecodeJSON(t, statusOut, &statusResp)
	if got := nestedString(t, statusResp, "data", "run", "status"); got != "done" {
		t.Fatalf("expected run status done, got %q", got)
	}
	tasks := nestedArray(t, statusResp, "data", "tasks")
	if len(tasks) != 1 {
		t.Fatalf("expected one task in status response, got %#v", tasks)
	}
	task, ok := tasks[0].(map[string]any)
	if !ok {
		t.Fatalf("expected task object, got %#v", tasks[0])
	}
	if got, _ := task["status"].(string); got != "done" {
		t.Fatalf("expected status task done, got %#v", task["status"])
	}
}

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

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

	runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"run", "init",
		"--run", "run_blog_002",
		"--goal", "Build dependency-aware workflow",
	)

	runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"task", "add",
		"--run", "run_blog_002",
		"--task", "T1",
		"--title", "Build backend",
		"--summary", "Implement backend APIs",
		"--default-to", "worker-a",
	)
	runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"task", "add",
		"--run", "run_blog_002",
		"--task", "T2",
		"--title", "Build frontend",
		"--summary", "Implement frontend flows",
		"--default-to", "worker-b",
	)
	runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"dep", "add",
		"--run", "run_blog_002",
		"--task", "T2",
		"--depends-on", "T1",
	)

	readyOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"ready",
		"--run", "run_blog_002",
	)

	var readyResp map[string]any
	mustDecodeJSON(t, readyOut, &readyResp)
	readyTasks := nestedArray(t, readyResp, "data", "tasks")
	if len(readyTasks) != 1 {
		t.Fatalf("expected only dependency-free task ready, got %#v", readyTasks)
	}
	readyTask, ok := readyTasks[0].(map[string]any)
	if !ok {
		t.Fatalf("expected ready task object, got %#v", readyTasks[0])
	}
	if taskID, _ := readyTask["task_id"].(string); taskID != "T1" {
		t.Fatalf("expected T1 ready before dependency clears, got %#v", readyTask["task_id"])
	}

	dispatchBackendOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"dispatch",
		"--run", "run_blog_002",
		"--task", "T1",
	)

	var dispatchBackendResp map[string]any
	mustDecodeJSON(t, dispatchBackendOut, &dispatchBackendResp)
	threadBackend := nestedString(t, dispatchBackendResp, "data", "attempt", "thread_id")

	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"claim",
		"--agent", "worker-a",
		"--thread", threadBackend,
	)
	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"done",
		"--agent", "worker-a",
		"--thread", threadBackend,
		"--summary", "Backend complete",
	)
	runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"reconcile",
		"--run", "run_blog_002",
	)

	readyAfterDoneOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"ready",
		"--run", "run_blog_002",
	)

	var readyAfterDoneResp map[string]any
	mustDecodeJSON(t, readyAfterDoneOut, &readyAfterDoneResp)
	readyTasks = nestedArray(t, readyAfterDoneResp, "data", "tasks")
	if len(readyTasks) != 1 {
		t.Fatalf("expected dependent task to become ready, got %#v", readyTasks)
	}
	readyTask, ok = readyTasks[0].(map[string]any)
	if !ok {
		t.Fatalf("expected ready task object, got %#v", readyTasks[0])
	}
	if taskID, _ := readyTask["task_id"].(string); taskID != "T2" {
		t.Fatalf("expected T2 ready after T1 completion, got %#v", readyTask["task_id"])
	}

	dispatchFrontendOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"dispatch",
		"--run", "run_blog_002",
		"--task", "T2",
	)

	var dispatchFrontendResp map[string]any
	mustDecodeJSON(t, dispatchFrontendOut, &dispatchFrontendResp)
	threadFrontend := nestedString(t, dispatchFrontendResp, "data", "attempt", "thread_id")

	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"claim",
		"--agent", "worker-b",
		"--thread", threadFrontend,
	)
	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"update",
		"--agent", "worker-b",
		"--thread", threadFrontend,
		"--status", "blocked",
		"--summary", "Need logging decision",
		"--payload-json", `{"question":"stdout or stderr?"}`,
	)
	runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"reconcile",
		"--run", "run_blog_002",
	)

	blockedOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"blocked",
		"--run", "run_blog_002",
	)

	var blockedResp map[string]any
	mustDecodeJSON(t, blockedOut, &blockedResp)
	blockedTasks := nestedArray(t, blockedResp, "data", "blocked")
	if len(blockedTasks) != 1 {
		t.Fatalf("expected one blocked task, got %#v", blockedTasks)
	}
	blockedTask, ok := blockedTasks[0].(map[string]any)
	if !ok {
		t.Fatalf("expected blocked task object, got %#v", blockedTasks[0])
	}
	question, ok := blockedTask["question"].(map[string]any)
	if !ok {
		t.Fatalf("expected blocked question object, got %#v", blockedTask["question"])
	}
	if kind, _ := question["kind"].(string); kind != "question" {
		t.Fatalf("expected blocked question kind, got %#v", question["kind"])
	}

	answerOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"answer",
		"--run", "run_blog_002",
		"--task", "T2",
		"--body", "Use stdout for MVP.",
	)

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

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

	var showResp map[string]any
	mustDecodeJSON(t, showOut, &showResp)
	messages := nestedArray(t, showResp, "data", "messages")
	if len(messages) < 4 {
		t.Fatalf("expected answer to append a message, got %#v", messages)
	}
	lastMessage, ok := messages[len(messages)-1].(map[string]any)
	if !ok {
		t.Fatalf("expected last message object, got %#v", messages[len(messages)-1])
	}
	if kind, _ := lastMessage["kind"].(string); kind != "answer" {
		t.Fatalf("expected latest message to be answer, got %#v", lastMessage["kind"])
	}

	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"update",
		"--agent", "worker-b",
		"--thread", threadFrontend,
		"--status", "in_progress",
		"--summary", "Decision applied",
	)
	runInboxCommand(
		t,
		"--db", dbPath,
		"--json",
		"done",
		"--agent", "worker-b",
		"--thread", threadFrontend,
		"--summary", "Frontend complete",
	)
	runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"reconcile",
		"--run", "run_blog_002",
	)

	statusOut := runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"status",
		"--run", "run_blog_002",
	)

	var statusResp map[string]any
	mustDecodeJSON(t, statusOut, &statusResp)
	if got := nestedString(t, statusResp, "data", "run", "status"); got != "done" {
		t.Fatalf("expected run status done after both tasks, got %q", got)
	}
}

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

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

	runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"run", "init",
		"--run", "run_blog_003",
		"--goal", "Validate ready gating",
	)
	runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"task", "add",
		"--run", "run_blog_003",
		"--task", "T1",
		"--title", "Backend",
	)
	runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"task", "add",
		"--run", "run_blog_003",
		"--task", "T2",
		"--title", "Frontend",
	)
	runOrchCommand(
		t,
		"--db", dbPath,
		"--json",
		"dep", "add",
		"--run", "run_blog_003",
		"--task", "T2",
		"--depends-on", "T1",
	)

	stdout, _, exitCode := executeOrchCommand(
		"--db", dbPath,
		"--json",
		"dispatch",
		"--run", "run_blog_003",
		"--task", "T2",
	)
	if exitCode != 30 {
		t.Fatalf("expected invalid_state exit code 30, got %d\nstdout:\n%s", exitCode, stdout)
	}
	assertErrorJSON(t, stdout, "invalid_state")
}
