package runner

import (
	"context"
	"strings"
	"testing"

	"github.com/easyai/easyai-ai-gateway/apps/api/internal/clients"
	"github.com/easyai/easyai-ai-gateway/apps/api/internal/store"
)

type namedClient string

func (namedClient) Run(context.Context, clients.Request) (clients.Response, error) {
	return clients.Response{}, nil
}

func TestValidateRequestAcceptsOpenAIReasoningEffort(t *testing.T) {
	for _, effort := range []string{"none", "minimal", "low", "medium", "high", "xhigh", "max"} {
		t.Run(effort, func(t *testing.T) {
			err := validateRequest("chat.completions", map[string]any{
				"messages":         []any{map[string]any{"role": "user", "content": "ping"}},
				"reasoning_effort": effort,
			})
			if err != nil {
				t.Fatalf("expected %s to be accepted: %v", effort, err)
			}
		})
	}
}

func TestValidateRequestRejectsNonOpenAIReasoningEffort(t *testing.T) {
	for _, effort := range []string{"auto"} {
		t.Run(effort, func(t *testing.T) {
			err := validateRequest("chat.completions", map[string]any{
				"messages":         []any{map[string]any{"role": "user", "content": "ping"}},
				"reasoning_effort": effort,
			})
			if err == nil {
				t.Fatalf("expected %s to be rejected", effort)
			}
			if clients.ErrorCode(err) != "invalid_parameter" || !strings.Contains(err.Error(), clients.OpenAIReasoningEffortValidationMessage) {
				t.Fatalf("unexpected validation error for %s: %T %v", effort, err, err)
			}
		})
	}
}

func TestClientForMapsGoogleGeminiSpecToGeminiClient(t *testing.T) {
	service := &Service{clients: map[string]clients.Client{
		"gemini": namedClient("gemini"),
		"openai": namedClient("openai"),
	}}

	candidates := []store.RuntimeModelCandidate{
		{SpecType: "google-gemini"},
		{SpecType: "openai", Provider: "gemini-openai"},
		{SpecType: "openai", BaseURL: "https://generativelanguage.googleapis.com/v1beta/openai"},
	}
	for _, candidate := range candidates {
		client := service.clientFor(candidate, false)
		if client != namedClient("gemini") {
			t.Fatalf("Gemini candidate should use gemini client, candidate=%+v got %T %[2]v", candidate, client)
		}
	}
}
