package probe

import (
	"context"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

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

	t.Run("responses unsupported but chat works", func(t *testing.T) {
		t.Parallel()

		var responseCalls int
		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			switch r.URL.Path {
			case "/v1/responses":
				responseCalls++
				http.Error(w, `{"error":"unsupported"}`, http.StatusForbidden)
			case "/v1/chat/completions":
				w.Header().Set("Content-Type", "application/json")
				_, _ = w.Write([]byte(`{"id":"chatcmpl-1","choices":[{"message":{"content":"pong"}}]}`))
			default:
				t.Fatalf("unexpected path %q", r.URL.Path)
			}
		}))
		defer server.Close()

		profile, err := ProbeCapabilities(context.Background(), server.URL, "sk-test", []string{"kimi-k2.6"})
		if err != nil {
			t.Fatalf("ProbeCapabilities() error = %v", err)
		}
		if !profile.TransportProfile.SupportsOpenAIChatCompletions {
			t.Fatal("SupportsOpenAIChatCompletions = false, want true")
		}
		if profile.TransportProfile.SupportsOpenAIResponses {
			t.Fatal("SupportsOpenAIResponses = true, want false")
		}
		if responseCalls == 0 {
			t.Fatal("responses endpoint was not probed")
		}
		if !containsString(profile.TransportProfile.KnownAdvisories, "responses_unsupported_but_chat_ok") {
			t.Fatalf("KnownAdvisories = %#v, want responses advisory", profile.TransportProfile.KnownAdvisories)
		}
	})

	t.Run("records per model capability profile", func(t *testing.T) {
		t.Parallel()

		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			switch r.URL.Path {
			case "/v1/responses":
				w.WriteHeader(http.StatusOK)
				_, _ = w.Write([]byte(`{"id":"resp_1"}`))
			case "/v1/chat/completions":
				body := make([]byte, r.ContentLength)
				_, _ = r.Body.Read(body)
				w.Header().Set("Content-Type", "application/json")
				_, _ = w.Write([]byte(`{"id":"chatcmpl-1","choices":[{"message":{"content":"ok"}}]}`))
			default:
				t.Fatalf("unexpected path %q", r.URL.Path)
			}
		}))
		defer server.Close()

		profile, err := ProbeCapabilities(context.Background(), server.URL, "sk-test", []string{"deepseek-ai/DeepSeek-V4-Pro", "kimi-k2.6"})
		if err != nil {
			t.Fatalf("ProbeCapabilities() error = %v", err)
		}
		if len(profile.ModelProfiles) != 2 {
			t.Fatalf("len(ModelProfiles) = %d, want 2", len(profile.ModelProfiles))
		}
		if profile.ModelProfiles[0].NormalizedModelID != "deepseek-v4-pro" {
			t.Fatalf("NormalizedModelID = %q, want %q", profile.ModelProfiles[0].NormalizedModelID, "deepseek-v4-pro")
		}
		if profile.ModelProfiles[0].CanonicalModelFamily != "deepseek-v4-pro" {
			t.Fatalf("CanonicalModelFamily = %q, want %q", profile.ModelProfiles[0].CanonicalModelFamily, "deepseek-v4-pro")
		}
		if !profile.ModelProfiles[0].SmokeChatOK {
			t.Fatal("SmokeChatOK = false, want true")
		}
	})

	t.Run("records initial probe advisory on transient auth race", func(t *testing.T) {
		t.Parallel()

		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			switch r.URL.Path {
			case "/v1/responses":
				http.Error(w, `{"error":"forbidden"}`, http.StatusForbidden)
			case "/v1/chat/completions":
				http.Error(w, `{"error":"warmup"}`, http.StatusForbidden)
			default:
				t.Fatalf("unexpected path %q", r.URL.Path)
			}
		}))
		defer server.Close()

		profile, err := ProbeCapabilities(context.Background(), server.URL, "sk-test", []string{"kimi-k2.6"})
		if err != nil {
			t.Fatalf("ProbeCapabilities() error = %v", err)
		}
		if !containsString(profile.TransportProfile.KnownAdvisories, "initial_probe_race_expected") {
			t.Fatalf("KnownAdvisories = %#v, want initial probe advisory", profile.TransportProfile.KnownAdvisories)
		}
	})
}

func containsString(values []string, want string) bool {
	for _, value := range values {
		if strings.TrimSpace(value) == want {
			return true
		}
	}
	return false
}
