package inbox

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

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

	dbPath := initCommandTestDB(t)

	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)

	if got := nestedString(t, sendResp, "data", "thread", "thread_id"); got == "" {
		t.Fatalf("expected thread_id, got empty")
	}
	if got := nestedString(t, sendResp, "data", "thread", "status"); got != "pending" {
		t.Fatalf("expected pending status, got %q", got)
	}
	if got := nestedString(t, sendResp, "data", "thread", "created_by"); got != "leader" {
		t.Fatalf("expected created_by leader, got %q", got)
	}
	if got := nestedString(t, sendResp, "data", "thread", "assigned_to"); got != "worker-a" {
		t.Fatalf("expected assigned_to worker-a, got %q", got)
	}
	if got := nestedString(t, sendResp, "data", "message", "kind"); got != "task" {
		t.Fatalf("expected message kind task, got %q", got)
	}
}

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

	dbPath := initCommandTestDB(t)
	threadID := sendPendingThread(t, dbPath, "leader", "worker-d", "Build editor", "Create editor v1")

	appendOut := 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.",
	)

	var appendResp map[string]any
	mustDecodeJSON(t, appendOut, &appendResp)
	if got := nestedString(t, appendResp, "data", "thread", "thread_id"); got != threadID {
		t.Fatalf("expected same thread_id %q, got %q", threadID, got)
	}
	if got := nestedString(t, appendResp, "data", "thread", "status"); got != "pending" {
		t.Fatalf("expected thread status to stay pending, got %q", got)
	}

	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"))
	}
}

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

	tempDir := t.TempDir()
	dbPath := filepath.Join(tempDir, "coord.db")
	bodyPath := filepath.Join(tempDir, "task.md")
	bodyContent := "Create the first editor screen.\nUse markdown syntax."
	if err := os.WriteFile(bodyPath, []byte(bodyContent), 0o644); err != nil {
		t.Fatalf("write body file: %v", err)
	}
	runInboxCommand(t, "--db", dbPath, "--json", "init")

	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,
	)

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

	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) != 1 {
		t.Fatalf("expected one message, got %#v", nestedValue(t, showResp, "data", "messages"))
	}
	message, ok := messages[0].(map[string]any)
	if !ok {
		t.Fatalf("expected message object, got %#v", messages[0])
	}
	if got, _ := message["body"].(string); got != bodyContent {
		t.Fatalf("expected body %q, got %#v", bodyContent, message["body"])
	}
}

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

	tempDir := t.TempDir()
	dbPath := filepath.Join(tempDir, "coord.db")
	artifactPath := filepath.Join(tempDir, "task.md")
	if err := os.WriteFile(artifactPath, []byte("task brief"), 0o644); err != nil {
		t.Fatalf("write artifact file: %v", err)
	}
	runInboxCommand(t, "--db", dbPath, "--json", "init")

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

	var sendResp map[string]any
	mustDecodeJSON(t, sendOut, &sendResp)

	artifacts, ok := nestedValue(t, sendResp, "data", "message", "artifacts").([]any)
	if !ok || len(artifacts) != 1 {
		t.Fatalf("expected one artifact, got %#v", nestedValue(t, sendResp, "data", "message", "artifacts"))
	}
	artifact, ok := artifacts[0].(map[string]any)
	if !ok {
		t.Fatalf("expected artifact object, got %#v", artifacts[0])
	}
	if got, _ := artifact["path"].(string); got != artifactPath {
		t.Fatalf("expected artifact path %q, got %#v", artifactPath, artifact["path"])
	}
	if got, _ := artifact["kind"].(string); got != "brief" {
		t.Fatalf("expected artifact kind brief, got %#v", artifact["kind"])
	}
	metadata, ok := artifact["metadata_json"].(map[string]any)
	if !ok {
		t.Fatalf("expected metadata_json object, got %#v", artifact["metadata_json"])
	}
	if got, _ := metadata["label"].(string); got != "task-brief" {
		t.Fatalf("expected metadata_json.label task-brief, got %#v", metadata["label"])
	}
}

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

	dbPath := initCommandTestDB(t)
	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 exit code 30, got %d", exitCode)
	}
	assertErrorJSON(t, stdout, "invalid_input")
}

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

	dbPath := initCommandTestDB(t)
	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 exit code 30, got %d", exitCode)
	}
	assertErrorJSON(t, stdout, "invalid_input")
}
