package app

import "testing"

func transcriptModel(roles ...string) Model {
	m := NewModel(nil)
	m.selectedID = "s1"
	msgs := make([]RouterEntry, len(roles))
	for i, r := range roles {
		msgs[i] = RouterEntry{Role: r, Content: r + itoa(i)}
	}
	m.routerMessages["s1"] = msgs
	return m
}

// ctrl+↑/↓ jumps between USER messages: from no selection ↑ grabs the most recent user
// message, keeps stepping to older ones (clamping), and ↓ steps back down, dropping to
// tail-follow (-1) once past the last user message.
func TestTranscriptNavUser(t *testing.T) {
	// indices:      0       1        2       3        4
	m := transcriptModel("user", "router", "user", "router", "user")

	if m.transcriptSel != -1 {
		t.Fatalf("initial sel = %d, want -1", m.transcriptSel)
	}
	m.transcriptNavUser(-1) // up from bottom → newest user (idx 4)
	if m.transcriptSel != 4 {
		t.Fatalf("up#1 = %d, want 4", m.transcriptSel)
	}
	m.transcriptNavUser(-1) // → user idx 2
	if m.transcriptSel != 2 {
		t.Fatalf("up#2 = %d, want 2", m.transcriptSel)
	}
	m.transcriptNavUser(-1) // → user idx 0
	if m.transcriptSel != 0 {
		t.Fatalf("up#3 = %d, want 0", m.transcriptSel)
	}
	m.transcriptNavUser(-1) // clamp at oldest
	if m.transcriptSel != 0 {
		t.Fatalf("up#4 (clamp) = %d, want 0", m.transcriptSel)
	}
	m.transcriptNavUser(1) // → user idx 2
	if m.transcriptSel != 2 {
		t.Fatalf("down#1 = %d, want 2", m.transcriptSel)
	}
	m.transcriptNavUser(1) // → user idx 4
	m.transcriptNavUser(1) // past last user → tail-follow
	if m.transcriptSel != -1 {
		t.Fatalf("down past last = %d, want -1 (tail-follow)", m.transcriptSel)
	}
}

// `y` copies the selected message, or the latest user/router turn when nothing is selected.
func TestCopyText(t *testing.T) {
	m := transcriptModel("user", "router", "tool")

	// no selection → most recent user/router turn (the "router" at idx 1, since "tool"
	// is skipped).
	if got := m.copyText(); got != "router1" {
		t.Fatalf("copyText (no sel) = %q, want %q", got, "router1")
	}

	m.transcriptSel = 0
	if got := m.copyText(); got != "user0" {
		t.Fatalf("copyText (sel idx 0) = %q, want %q", got, "user0")
	}
}
