package anthropic_test

import (
	"cursor-api-proxy/internal/anthropic"
	"strings"
	"testing"
)

func TestBuildPromptFromAnthropicMessages_Simple(t *testing.T) {
	messages := []anthropic.MessageParam{
		{Role: "user", Content: "Hello"},
		{Role: "assistant", Content: "Hi there"},
	}
	prompt := anthropic.BuildPromptFromAnthropicMessages(messages, nil)
	if !strings.Contains(prompt, "Hello") {
		t.Errorf("prompt missing user message: %q", prompt)
	}
	if !strings.Contains(prompt, "Hi there") {
		t.Errorf("prompt missing assistant message: %q", prompt)
	}
}

func TestBuildPromptFromAnthropicMessages_WithSystem(t *testing.T) {
	messages := []anthropic.MessageParam{
		{Role: "user", Content: "ping"},
	}
	prompt := anthropic.BuildPromptFromAnthropicMessages(messages, "You are a helpful bot.")
	if !strings.Contains(prompt, "You are a helpful bot.") {
		t.Errorf("prompt missing system: %q", prompt)
	}
	if !strings.Contains(prompt, "ping") {
		t.Errorf("prompt missing user: %q", prompt)
	}
}

func TestBuildPromptFromAnthropicMessages_SystemArray(t *testing.T) {
	system := []interface{}{
		map[string]interface{}{"type": "text", "text": "Part A"},
		map[string]interface{}{"type": "text", "text": "Part B"},
	}
	messages := []anthropic.MessageParam{
		{Role: "user", Content: "test"},
	}
	prompt := anthropic.BuildPromptFromAnthropicMessages(messages, system)
	if !strings.Contains(prompt, "Part A") {
		t.Errorf("prompt missing Part A: %q", prompt)
	}
	if !strings.Contains(prompt, "Part B") {
		t.Errorf("prompt missing Part B: %q", prompt)
	}
}

func TestBuildPromptFromAnthropicMessages_ContentBlocks(t *testing.T) {
	content := []interface{}{
		map[string]interface{}{"type": "text", "text": "block one"},
		map[string]interface{}{"type": "text", "text": "block two"},
	}
	messages := []anthropic.MessageParam{
		{Role: "user", Content: content},
	}
	prompt := anthropic.BuildPromptFromAnthropicMessages(messages, nil)
	if !strings.Contains(prompt, "block one") {
		t.Errorf("prompt missing 'block one': %q", prompt)
	}
	if !strings.Contains(prompt, "block two") {
		t.Errorf("prompt missing 'block two': %q", prompt)
	}
}

func TestBuildPromptFromAnthropicMessages_ImageBlock(t *testing.T) {
	content := []interface{}{
		map[string]interface{}{
			"type": "image",
			"source": map[string]interface{}{
				"type":       "base64",
				"media_type": "image/png",
				"data":       "abc123",
			},
		},
	}
	messages := []anthropic.MessageParam{
		{Role: "user", Content: content},
	}
	prompt := anthropic.BuildPromptFromAnthropicMessages(messages, nil)
	if !strings.Contains(prompt, "[Image") {
		t.Errorf("prompt missing [Image]: %q", prompt)
	}
}

func TestBuildPromptFromAnthropicMessages_EmptyContentSkipped(t *testing.T) {
	messages := []anthropic.MessageParam{
		{Role: "user", Content: ""},
		{Role: "assistant", Content: "response"},
	}
	prompt := anthropic.BuildPromptFromAnthropicMessages(messages, nil)
	if !strings.Contains(prompt, "response") {
		t.Errorf("prompt missing 'response': %q", prompt)
	}
}

func TestBuildPromptFromAnthropicMessages_UnknownRoleBecomesUser(t *testing.T) {
	messages := []anthropic.MessageParam{
		{Role: "system", Content: "system-as-user"},
	}
	prompt := anthropic.BuildPromptFromAnthropicMessages(messages, nil)
	if !strings.Contains(prompt, "system-as-user") {
		t.Errorf("prompt missing 'system-as-user': %q", prompt)
	}
}
