package apimart

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

	"juhe-factory/api/internal/provider"
)

func TestChatWithUsageReadsOpenAICompatibleAndNestedUsage(t *testing.T) {
	cases := []struct {
		name     string
		response string
		content  string
		input    int64
		output   int64
		total    int64
		finish   string
	}{
		{name: "top level", response: `{"choices":[{"message":{"content":"result"},"finish_reason":"stop"}],"usage":{"prompt_tokens":120,"completion_tokens":30,"total_tokens":150}}`, content: "result", input: 120, output: 30, total: 150, finish: "stop"},
		{name: "nested alternate names", response: `{"data":{"choices":[{"message":{"content":[{"type":"text","text":"part one"},{"type":"text","text":"part two"}]}}],"usage":{"input_tokens":44,"output_tokens":11}}}`, content: "part one\npart two", input: 44, output: 11, total: 55},
	}
	for _, test := range cases {
		t.Run(test.name, func(t *testing.T) {
			server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
				if request.URL.Path != "/chat/completions" {
					t.Fatalf("unexpected path: %s", request.URL.Path)
				}
				response.Header().Set("Content-Type", "application/json")
				fmt.Fprint(response, test.response)
			}))
			defer server.Close()
			result, err := NewClient(server.Client()).ChatWithUsage(context.Background(), server.URL, "key", map[string]any{"model": "text-model"})
			if err != nil {
				t.Fatalf("ChatWithUsage returned error: %v", err)
			}
			if result.Content != test.content || result.InputTokens != test.input || result.OutputTokens != test.output || result.TotalTokens != test.total || result.FinishReason != test.finish {
				t.Fatalf("unexpected result: %#v", result)
			}
		})
	}
}

func TestChatWithUsageReturnsExplicitUpstreamFailure(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, _ *http.Request) {
		response.WriteHeader(http.StatusBadGateway)
		fmt.Fprint(response, `{"error":"relay unavailable"}`)
	}))
	defer server.Close()
	_, err := NewClient(server.Client()).ChatWithUsage(context.Background(), server.URL, "key", map[string]any{"model": "text-model"})
	httpErr, ok := err.(*provider.HTTPError)
	if !ok || httpErr.StatusCode != http.StatusBadGateway {
		t.Fatalf("expected HTTPError with relay status, got %T %v", err, err)
	}
}
