package compat

import (
	"context"
	"encoding/json"
	"io"
	"log/slog"
	"net/http"
	"net/http/httptest"
	"sync"
	"testing"
)

func newTestClient(t *testing.T, url string) *Client {
	t.Helper()
	return New(Config{
		Name:       "litellm",
		BaseURL:    url,
		APIKey:     "test-key",
		HTTPClient: http.DefaultClient,
		Log:        slog.New(slog.NewTextHandler(io.Discard, nil)),
	})
}

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

	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			_ = r.Body.Close()
		}()

		var req chatCompletionsRequest
		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
			t.Fatalf("decode request: %v", err)
		}
		if req.Stream == nil || !*req.Stream {
			t.Fatalf("stream flag = %v, want true", req.Stream)
		}
		if req.Model != "qwen3.5:latest" {
			t.Fatalf("model = %q, want qwen3.5:latest", req.Model)
		}

		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"{\\\"people\\\":[],\"}}]}\n\n")
		_, _ = io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"\\\"action_items\\\":[],\\\"dates_mentioned\\\":[],\"}}]}\n\n")
		_, _ = io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"\\\"topics\\\":[\\\"android\\\"],\\\"type\\\":\\\"idea\\\",\\\"source\\\":\\\"stream\\\"}\"}}]}\n\n")
		_, _ = io.WriteString(w, "data: [DONE]\n\n")
	}))
	defer server.Close()

	client := newTestClient(t, server.URL)
	metadata, err := client.ExtractMetadataWith(context.Background(), MetadataOptions{
		Model:       "qwen3.5:latest",
		Temperature: 0.1,
	}, "Project idea: Build an Android companion app.")
	if err != nil {
		t.Fatalf("ExtractMetadataWith() error = %v", err)
	}

	if metadata.Type != "idea" {
		t.Fatalf("metadata type = %q, want idea", metadata.Type)
	}
	if metadata.Source != "stream" {
		t.Fatalf("metadata source = %q, want stream", metadata.Source)
	}
	if len(metadata.Topics) != 1 || metadata.Topics[0] != "android" {
		t.Fatalf("metadata topics = %#v, want [android]", metadata.Topics)
	}
}

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

	var mu sync.Mutex
	jsonModeCalls := 0
	plainCalls := 0

	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			_ = r.Body.Close()
		}()

		var req chatCompletionsRequest
		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
			t.Fatalf("decode request: %v", err)
		}

		if req.ResponseFormat != nil && req.ResponseFormat.Type == "json_object" {
			mu.Lock()
			jsonModeCalls++
			mu.Unlock()
			_, _ = io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":""}}]}`)
			return
		}

		mu.Lock()
		plainCalls++
		mu.Unlock()
		_, _ = io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"{\"people\":[],\"action_items\":[],\"dates_mentioned\":[],\"topics\":[\"android\"],\"type\":\"idea\",\"source\":\"test\"}"}}]}`)
	}))
	defer server.Close()

	client := newTestClient(t, server.URL)
	metadata, err := client.ExtractMetadataWith(context.Background(), MetadataOptions{
		Model:       "qwen3.5:latest",
		Temperature: 0.1,
	}, "Project idea: Build an Android companion app.")
	if err != nil {
		t.Fatalf("ExtractMetadataWith() error = %v", err)
	}

	if metadata.Type != "idea" {
		t.Fatalf("metadata type = %q, want idea", metadata.Type)
	}
	if metadata.Source != "test" {
		t.Fatalf("metadata source = %q, want test", metadata.Source)
	}

	mu.Lock()
	defer mu.Unlock()
	if jsonModeCalls != maxMetadataAttempts {
		t.Fatalf("json mode calls = %d, want %d", jsonModeCalls, maxMetadataAttempts)
	}
	if plainCalls != 1 {
		t.Fatalf("plain calls = %d, want 1", plainCalls)
	}
}

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

	cases := []struct {
		name string
		err  error
		want bool
	}{
		{"nil", nil, false},
		{"invalid model", errMsg("Invalid model name passed in model=qwen3"), true},
		{"model not found", errMsg("model_not_found"), true},
		{"no such model", errMsg("no such model"), true},
		{"transient", errMsg("connection refused"), false},
	}

	for _, tc := range cases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			if got := IsPermanentModelError(tc.err); got != tc.want {
				t.Fatalf("IsPermanentModelError(%v) = %v, want %v", tc.err, got, tc.want)
			}
		})
	}
}

type stringError string

func (s stringError) Error() string { return string(s) }

func errMsg(s string) error { return stringError(s) }
