package render

import (
	"strings"
	"testing"

	"github.com/mark3labs/kit/internal/ui/style"
)

func TestHighlightFileTokens(t *testing.T) {
	theme := style.DefaultTheme()

	tests := []struct {
		name     string
		input    string
		wantHas  []string // substrings that must be present in the output
		wantNone []string // substrings that must NOT be present as plain text
	}{
		{
			name:    "no tokens",
			input:   "hello world",
			wantHas: []string{"hello world"},
		},
		{
			name:    "single unquoted token",
			input:   "refactor @main.go please",
			wantHas: []string{"@main.go", "refactor", "please"},
		},
		{
			name:    "quoted token with spaces",
			input:   `check @"path with spaces/file.txt" out`,
			wantHas: []string{`@"path with spaces/file.txt"`, "check", "out"},
		},
		{
			name:    "multiple tokens",
			input:   "@main.go @utils.go refactor these",
			wantHas: []string{"@main.go", "@utils.go", "refactor these"},
		},
		{
			name:    "path with directory",
			input:   "look at @internal/ui/render/blocks.go",
			wantHas: []string{"@internal/ui/render/blocks.go", "look at"},
		},
		{
			name:    "empty string",
			input:   "",
			wantHas: []string{""},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := HighlightFileTokens(tt.input, theme)

			for _, want := range tt.wantHas {
				if !strings.Contains(result, want) {
					t.Errorf("HighlightFileTokens(%q) = %q, want substring %q", tt.input, result, want)
				}
			}

			// If there were @tokens, the result should contain ANSI escape
			// sequences (from lipgloss styling).
			if fileTokenPattern.MatchString(tt.input) && !strings.Contains(result, "\x1b[") {
				t.Errorf("HighlightFileTokens(%q) should contain ANSI escapes for @tokens but got %q", tt.input, result)
			}
		})
	}
}

// TestHighlightFileTokensInjectsANSI verifies that HighlightFileTokens
// preserves the original @file references in the output and wraps each
// token with ANSI escape codes for the theme accent color.
func TestHighlightFileTokensInjectsANSI(t *testing.T) {
	theme := style.DefaultTheme()

	content := "refactor @main.go and @utils.go"
	result := HighlightFileTokens(content, theme)

	// The output should still contain both file references.
	if !strings.Contains(result, "@main.go") {
		t.Errorf("HighlightFileTokens output should contain @main.go, got:\n%s", result)
	}
	if !strings.Contains(result, "@utils.go") {
		t.Errorf("HighlightFileTokens output should contain @utils.go, got:\n%s", result)
	}

	// Verify ANSI codes are present (the tokens are styled).
	if !strings.Contains(result, "\x1b[") {
		t.Errorf("HighlightFileTokens output should contain ANSI escape codes for styled @file tokens")
	}
}
