package service

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"code.chimeric.al/chimerical/odidere/internal/llm"
)

func TestStatusHandler(t *testing.T) {
	svc := &Service{}

	req := httptest.NewRequest(http.MethodGet, "/status", nil)
	w := httptest.NewRecorder()

	svc.status(w, req)

	if w.Code != http.StatusOK {
		t.Errorf(
			"status handler returned %d, want %d",
			w.Code,
			http.StatusOK,
		)
	}
}

func TestResponseIncludesStatsInJSON(t *testing.T) {
	resp := Response{
		Messages: []llm.Message{
			{Role: llm.RoleAssistant, ContentParts: []llm.ContentPart{{
				Type: llm.ContentTypeText,
				Text: "hello",
			}}},
		},
		Provider: "test-provider",
		Model:    "test-model",
		Usage: &llm.Usage{
			PromptTokens:     10,
			CompletionTokens: 5,
			TotalTokens:      15,
			PromptTokensDetails: &llm.TokenDetails{
				CachedTokens: 3,
			},
			CompletionTokensDetails: &llm.TokenDetails{
				ReasoningTokens: 2,
			},
		},
		Timings: &llm.Timings{
			PromptMS:           50.5,
			PromptPerSecond:    198.0,
			PredictedMS:        100.0,
			PredictedPerSecond: 50.0,
		},
	}

	// Verify JSON encoding includes stats fields
	var buf strings.Builder
	enc := json.NewEncoder(&buf)
	if err := enc.Encode(resp); err != nil {
		t.Fatalf("encode Response: %v", err)
	}

	jsonStr := buf.String()
	if !strings.Contains(jsonStr, `"prompt_tokens":10`) {
		t.Errorf("JSON missing prompt_tokens: %s", jsonStr)
	}
	if !strings.Contains(jsonStr, `"completion_tokens":5`) {
		t.Errorf("JSON missing completion_tokens: %s", jsonStr)
	}
	if !strings.Contains(jsonStr, `"total_tokens":15`) {
		t.Errorf("JSON missing total_tokens: %s", jsonStr)
	}
	if !strings.Contains(jsonStr, `"prompt_ms":50.5`) {
		t.Errorf("JSON missing prompt_ms: %s", jsonStr)
	}
	if !strings.Contains(jsonStr, `"predicted_ms":100`) {
		t.Errorf("JSON missing predicted_ms: %s", jsonStr)
	}
}

func TestStreamMessageIncludesUsageInJSON(t *testing.T) {
	msg := StreamMessage{
		Delta:    "hello",
		Provider: "test-provider",
		Model:    "test-model",
		Usage: &llm.Usage{
			PromptTokens:     10,
			CompletionTokens: 5,
			TotalTokens:      15,
		},
	}

	var buf strings.Builder
	enc := json.NewEncoder(&buf)
	if err := enc.Encode(msg); err != nil {
		t.Fatalf("encode StreamMessage: %v", err)
	}

	jsonStr := buf.String()
	if !strings.Contains(jsonStr, `"prompt_tokens":10`) {
		t.Errorf("JSON missing prompt_tokens: %s", jsonStr)
	}
	if !strings.Contains(jsonStr, `"total_tokens":15`) {
		t.Errorf("JSON missing total_tokens: %s", jsonStr)
	}
}
