package providertests

import (
	"net/http"
	"os"
	"testing"

	"charm.land/fantasy"
	"charm.land/fantasy/providers/anthropic"
	"charm.land/fantasy/providers/openrouter"
	"charm.land/x/vcr"
	"github.com/stretchr/testify/require"
)

var openrouterTestModels = []testModel{
	{"anthropic", "anthropic/claude-haiku-4.5", false},
	{"openai", "openai/gpt-5.4-nano", false},
	{"gemini", "google/gemini-3.1-flash-lite-preview", false},
	{"grok", "x-ai/grok-4.1-fast", false},
	{"glm", "z-ai/glm-5-turbo", false},
	{"kimi", "moonshotai/kimi-k2.5", false},
}

func TestOpenRouterCommon(t *testing.T) {
	var pairs []builderPair
	for _, m := range openrouterTestModels {
		pairs = append(pairs, builderPair{m.name, openrouterBuilder(m.model), nil, nil})
	}
	testCommon(t, pairs)
}

func TestOpenRouterCommonWithAnthropicCache(t *testing.T) {
	testCommon(t, []builderPair{
		{"anthropic", openrouterBuilder("anthropic/claude-haiku-4.5"), nil, addAnthropicCaching},
	})
}

func TestOpenRouterThinking(t *testing.T) {
	opts := fantasy.ProviderOptions{
		openrouter.Name: &openrouter.ProviderOptions{
			Reasoning: &openrouter.ReasoningOptions{
				Effort: openrouter.ReasoningEffortOption(openrouter.ReasoningEffortMedium),
			},
		},
	}

	var pairs []builderPair
	for _, m := range openrouterTestModels {
		if !m.reasoning {
			continue
		}
		pairs = append(pairs, builderPair{m.name, openrouterBuilder(m.model), opts, nil})
	}
	testThinking(t, pairs, testOpenrouterThinking)

	// test anthropic signature
	testThinking(t, []builderPair{
		{"anthropic", openrouterBuilder("anthropic/claude-haiku-4.5"), opts, nil},
	}, testOpenrouterThinkingWithSignature)
}

func testOpenrouterThinkingWithSignature(t *testing.T, result *fantasy.AgentResult) {
	reasoningContentCount := 0
	signaturesCount := 0
	// Test if we got the signature
	for _, step := range result.Steps {
		for _, msg := range step.Messages {
			for _, content := range msg.Content {
				if content.GetType() == fantasy.ContentTypeReasoning {
					reasoningContentCount += 1
					reasoningContent, ok := fantasy.AsContentType[fantasy.ReasoningPart](content)
					if !ok {
						continue
					}
					if len(reasoningContent.ProviderOptions) == 0 {
						continue
					}

					anthropicReasoningMetadata, ok := reasoningContent.ProviderOptions[anthropic.Name]
					if !ok {
						continue
					}
					if reasoningContent.Text != "" {
						if typed, ok := anthropicReasoningMetadata.(*anthropic.ReasoningOptionMetadata); ok {
							require.NotEmpty(t, typed.Signature)
							signaturesCount += 1
						}
					}
				}
			}
		}
	}
	require.Greater(t, reasoningContentCount, 0)
	require.Greater(t, signaturesCount, 0)
	require.Equal(t, reasoningContentCount, signaturesCount)
	// we also add the anthropic metadata so test that
	testAnthropicThinking(t, result)
}

func testOpenrouterThinking(t *testing.T, result *fantasy.AgentResult) {
	reasoningContentCount := 0
	for _, step := range result.Steps {
		for _, msg := range step.Messages {
			for _, content := range msg.Content {
				if content.GetType() == fantasy.ContentTypeReasoning {
					reasoningContentCount += 1
				}
			}
		}
	}
	require.Greater(t, reasoningContentCount, 0)
}

func openrouterBuilder(model string) builderFunc {
	return func(t *testing.T, r *vcr.Recorder) (fantasy.LanguageModel, error) {
		provider, err := openrouter.New(
			openrouter.WithAPIKey(os.Getenv("FANTASY_OPENROUTER_API_KEY")),
			openrouter.WithHTTPClient(&http.Client{Transport: r}),
		)
		if err != nil {
			return nil, err
		}
		return provider.LanguageModel(t.Context(), model)
	}
}
