package probe

import (
	"context"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestResolveSmokeModel(t *testing.T) {
	t.Parallel()

	t.Run("uses requested alias when matched", func(t *testing.T) {
		t.Parallel()

		profile := &CapabilityProfile{
			ModelProfiles: []ModelCapabilityProfile{
				{RawModelID: "kimi-k2.6", CanonicalModelFamily: "kimi-2.6", SmokeChatOK: true},
			},
		}

		model, recommended, err := ResolveSmokeModel([]string{"kimi 2.6"}, []string{"kimi-k2.6"}, profile)
		if err != nil {
			t.Fatalf("ResolveSmokeModel() error = %v", err)
		}
		if model != "kimi-k2.6" {
			t.Fatalf("ResolveSmokeModel() model = %q, want %q", model, "kimi-k2.6")
		}
		if len(recommended) != 1 || recommended[0] != "kimi-k2.6" {
			t.Fatalf("recommended = %#v, want discovered alias", recommended)
		}
	})

	t.Run("falls back to discovered model with smoke support", func(t *testing.T) {
		t.Parallel()

		profile := &CapabilityProfile{
			ModelProfiles: []ModelCapabilityProfile{
				{RawModelID: "deepseek-ai/DeepSeek-V4-Pro", CanonicalModelFamily: "deepseek-v4-pro", SmokeChatOK: true},
			},
		}

		model, recommended, err := ResolveSmokeModel([]string{"unknown"}, []string{"deepseek-ai/DeepSeek-V4-Pro"}, profile)
		if err != nil {
			t.Fatalf("ResolveSmokeModel() error = %v", err)
		}
		if model != "deepseek-ai/DeepSeek-V4-Pro" {
			t.Fatalf("ResolveSmokeModel() model = %q, want discovered model", model)
		}
		if len(recommended) != 0 {
			t.Fatalf("recommended = %#v, want empty for unknown request", recommended)
		}
	})
}

func TestSmokeCompletion(t *testing.T) {
	t.Parallel()

	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/v1/chat/completions" {
			t.Fatalf("path = %q, want chat completions fallback", r.URL.Path)
		}
		var payload map[string]any
		if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
			t.Fatalf("decode request body: %v", err)
		}
		if payload["model"] != "kimi-k2.6" {
			t.Fatalf("payload model = %v, want kimi-k2.6", payload["model"])
		}
		w.Header().Set("Content-Type", "application/json")
		_, _ = w.Write([]byte(`{"id":"chatcmpl-1","choices":[{"message":{"content":"pong"}}]}`))
	}))
	defer server.Close()

	profile := &CapabilityProfile{
		TransportProfile: TransportProfile{
			SupportsOpenAIChatCompletions: true,
			SupportsOpenAIResponses:       false,
			KnownAdvisories:               []string{"responses_unsupported_but_chat_ok"},
		},
	}

	result, err := SmokeCompletion(context.Background(), server.URL, "sk-test", "kimi-k2.6", profile)
	if err != nil {
		t.Fatalf("SmokeCompletion() error = %v", err)
	}
	if result.HTTPStatus != http.StatusOK {
		t.Fatalf("HTTPStatus = %d, want %d", result.HTTPStatus, http.StatusOK)
	}
	if result.Classification != "chat_completions" {
		t.Fatalf("Classification = %q, want %q", result.Classification, "chat_completions")
	}
}
