package models

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

func newMoonshotServer(t *testing.T, handler func(t *testing.T, r *http.Request, body map[string]interface{}, w http.ResponseWriter)) *httptest.Server {
	t.Helper()
	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if got := r.Header.Get("Authorization"); got != "Bearer test-key" {
			t.Errorf("expected Authorization=Bearer test-key, got %q", got)
			return
		}

		var body map[string]interface{}
		if r.Method == http.MethodPost {
			if got := r.Header.Get("Content-Type"); !strings.HasPrefix(got, "application/json") {
				t.Errorf("expected Content-Type to start with application/json, got %q", got)
				return
			}
			raw, err := io.ReadAll(r.Body)
			if err != nil {
				t.Errorf("read body: %v", err)
				return
			}
			if err := json.Unmarshal(raw, &body); err != nil {
				t.Errorf("unmarshal: %v\nraw=%s", err, string(raw))
				return
			}
		} else {
			if r.ContentLength > 0 {
				t.Errorf("expected %s request without body, ContentLength=%d", r.Method, r.ContentLength)
				return
			}
			raw, err := io.ReadAll(r.Body)
			if err != nil {
				t.Errorf("read body: %v", err)
				return
			}
			if len(raw) != 0 {
				t.Errorf("expected %s request without body, got %q", r.Method, string(raw))
				return
			}
		}

		handler(t, r, body, w)
	}))
}

func newMoonshotForTest(baseURL string) *MoonshotModel {
	return NewMoonshotModel(
		map[string]string{"default": baseURL},
		URLSuffix{
			Chat:    "chat/completions",
			Models:  "models",
			Balance: "users/me/balance",
		},
	)
}

func TestMoonshotNewInstancePreservesConfig(t *testing.T) {
	model := NewMoonshotModel(
		map[string]string{"default": "http://old.example"},
		URLSuffix{Chat: "chat", Models: "models", Balance: "balance"},
	)

	instance, ok := model.NewInstance(map[string]string{"default": "http://new.example"}).(*MoonshotModel)
	if !ok {
		t.Fatalf("NewInstance type=%T, want *MoonshotModel", instance)
	}
	if instance.baseModel.BaseURL["default"] != "http://new.example" {
		t.Errorf("BaseURL=%q", instance.baseModel.BaseURL["default"])
	}
	if instance.baseModel.URLSuffix.Chat != "chat" || instance.baseModel.URLSuffix.Models != "models" || instance.baseModel.URLSuffix.Balance != "balance" {
		t.Errorf("URLSuffix=%+v", instance.baseModel.URLSuffix)
	}
	if instance.baseModel.httpClient == nil {
		t.Error("httpClient is nil")
	}
}

func TestMoonshotChatForcesNonStreaming(t *testing.T) {
	withSSRFBypass(t)
	ctx := t.Context()
	srv := newMoonshotServer(t, func(t *testing.T, r *http.Request, body map[string]interface{}, w http.ResponseWriter) {
		if r.Method != http.MethodPost {
			t.Errorf("method=%s, want POST", r.Method)
		}
		if r.URL.Path != "/chat/completions" {
			t.Errorf("path=%s, want /chat/completions", r.URL.Path)
		}
		if body["model"] != "kimi-k2.6" {
			t.Errorf("model=%v, want kimi-k2.6", body["model"])
		}
		if body["stream"] != false {
			t.Errorf("stream=%v, want false", body["stream"])
		}
		_ = json.NewEncoder(w).Encode(map[string]interface{}{
			"choices": []map[string]interface{}{{
				"message": map[string]interface{}{
					"content":           "pong",
					"reasoning_content": "\nthought",
				},
			}},
		})
	})
	defer srv.Close()

	apiKey := " test-key "
	stream := true
	thinking := true
	resp, err := newMoonshotForTest(srv.URL).ChatWithMessages(
		ctx,
		" kimi-k2.6 ",
		[]Message{{Role: "user", Content: "ping"}},
		&APIConfig{ApiKey: &apiKey},
		&ChatConfig{Stream: &stream, Thinking: &thinking}, nil,
	)
	if err != nil {
		t.Fatalf("ChatWithMessages: %v", err)
	}
	if resp.Answer == nil || *resp.Answer != "pong" {
		t.Errorf("Answer=%v, want pong", resp.Answer)
	}
	if resp.ReasonContent == nil || *resp.ReasonContent != "thought" {
		t.Errorf("ReasonContent=%v, want thought", resp.ReasonContent)
	}
}

func TestMoonshotChatSupportsTools(t *testing.T) {
	withSSRFBypass(t)
	ctx := t.Context()
	srv := newMoonshotServer(t, func(t *testing.T, _ *http.Request, body map[string]interface{}, w http.ResponseWriter) {
		tools, ok := body["tools"].([]interface{})
		if !ok || len(tools) != 1 {
			t.Errorf("tools=%#v, want one tool", body["tools"])
		}
		if body["tool_choice"] != "auto" {
			t.Errorf("tool_choice=%v, want auto", body["tool_choice"])
		}
		_ = json.NewEncoder(w).Encode(map[string]interface{}{
			"choices": []map[string]interface{}{{
				"message": map[string]interface{}{
					"content": nil,
					"tool_calls": []map[string]interface{}{{
						"id":   "call_weather",
						"type": "function",
						"function": map[string]string{
							"name":      "get_weather",
							"arguments": `{"city":"北京"}`,
						},
					}},
				},
			}},
		})
	})
	defer srv.Close()

	apiKey := "test-key"
	tools := []map[string]interface{}{{
		"type": "function",
		"function": map[string]interface{}{
			"name": "get_weather",
		},
	}}
	resp, err := newMoonshotForTest(srv.URL).ChatWithMessages(
		ctx,
		"kimi-k2.6",
		[]Message{{Role: "user", Content: "北京今天天气怎么样？"}},
		&APIConfig{ApiKey: &apiKey},
		&ChatConfig{Tools: tools}, nil,
	)
	if err != nil {
		t.Fatalf("ChatWithMessages: %v", err)
	}
	if resp.Answer == nil || *resp.Answer != "" {
		t.Errorf("Answer=%v, want empty", resp.Answer)
	}
	if len(resp.ToolCalls) != 1 || resp.ToolCalls[0]["id"] != "call_weather" {
		t.Errorf("ToolCalls=%#v, want call_weather", resp.ToolCalls)
	}
}

func TestMoonshotChatParsesCompletionSchema(t *testing.T) {
	withSSRFBypass(t)
	ctx := t.Context()
	srv := newMoonshotServer(t, func(t *testing.T, _ *http.Request, _ map[string]interface{}, w http.ResponseWriter) {
		_ = json.NewEncoder(w).Encode(map[string]interface{}{
			"id":      "cmpl_1",
			"object":  "chat.completion",
			"created": 1774598400,
			"model":   "kimi-k2.6",
			"choices": []map[string]interface{}{{
				"index": 0,
				"message": map[string]interface{}{
					"role":              "assistant",
					"content":           nil,
					"reasoning_content": "\nreason",
					"tool_calls": []map[string]interface{}{{
						"id":   "call_1",
						"type": "function",
						"function": map[string]interface{}{
							"name":      "lookup",
							"arguments": `{"q":"moonshot"}`,
						},
					}},
				},
				"finish_reason": "tool_calls",
			}},
			"usage": map[string]interface{}{
				"prompt_tokens":     10,
				"completion_tokens": 5,
				"total_tokens":      15,
				"cached_tokens":     3,
			},
		})
	})
	defer srv.Close()

	apiKey := "test-key"
	thinking := true
	resp, err := newMoonshotForTest(srv.URL).ChatWithMessages(
		ctx,
		"kimi-k2.6",
		[]Message{{Role: "user", Content: "ping"}},
		&APIConfig{ApiKey: &apiKey},
		&ChatConfig{Thinking: &thinking}, nil,
	)
	if err != nil {
		t.Fatalf("ChatWithMessages: %v", err)
	}
	if resp.Answer == nil || *resp.Answer != "" {
		t.Errorf("Answer=%v, want empty", resp.Answer)
	}
	if resp.ReasonContent == nil || *resp.ReasonContent != "reason" {
		t.Errorf("ReasonContent=%v, want reason", resp.ReasonContent)
	}
	if len(resp.ToolCalls) != 1 {
		t.Fatalf("ToolCalls=%#v, want one", resp.ToolCalls)
	}
	function, ok := resp.ToolCalls[0]["function"].(map[string]any)
	if !ok || function["name"] != "lookup" || function["arguments"] != `{"q":"moonshot"}` {
		t.Errorf("function=%#v", resp.ToolCalls[0]["function"])
	}
	if resp.Usage == nil || resp.Usage.PromptTokens != 10 || resp.Usage.CompletionTokens != 5 || resp.Usage.TotalTokens != 15 {
		t.Errorf("Usage=%#v, want 10/5/15", resp.Usage)
	}
}

func TestMoonshotStreamForcesStreaming(t *testing.T) {
	withSSRFBypass(t)
	ctx := t.Context()
	srv := newMoonshotServer(t, func(t *testing.T, r *http.Request, body map[string]interface{}, w http.ResponseWriter) {
		if r.Method != http.MethodPost {
			t.Errorf("method=%s, want POST", r.Method)
		}
		if r.URL.Path != "/chat/completions" {
			t.Errorf("path=%s, want /chat/completions", r.URL.Path)
		}
		if body["stream"] != true {
			t.Errorf("stream=%v, want true", body["stream"])
		}
		if got := r.Header.Get("Accept"); got != "text/event-stream" {
			t.Errorf("Accept=%q, want text/event-stream", got)
		}
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = io.WriteString(w, strings.Join([]string{
			`data: {"choices":[{"delta":{"reasoning_content":"thinking"}}]}`,
			`data: {"choices":[{"delta":{"content":"hello"}}]}`,
			`data: [DONE]`,
			``,
		}, "\n"))
	})
	defer srv.Close()

	apiKey := "test-key"
	stream := false
	var content, reasoning []string
	var sawDone bool
	err := newMoonshotForTest(srv.URL).ChatStreamlyWithSender(
		ctx,
		"kimi-k2.6",
		[]Message{{Role: "user", Content: "ping"}},
		&APIConfig{ApiKey: &apiKey},
		&ChatConfig{Stream: &stream},
		nil,
		func(answer, reason *string) error {
			if answer != nil {
				if *answer == "[DONE]" {
					sawDone = true
					return nil
				}
				content = append(content, *answer)
			}
			if reason != nil {
				reasoning = append(reasoning, *reason)
			}
			return nil
		},
	)
	if err != nil {
		t.Fatalf("ChatStreamlyWithSender: %v", err)
	}
	if got := strings.Join(content, ""); got != "hello" {
		t.Errorf("content=%q, want hello", got)
	}
	if got := strings.Join(reasoning, ""); got != "thinking" {
		t.Errorf("reasoning=%q, want thinking", got)
	}
	if !sawDone {
		t.Error("expected [DONE] sentinel")
	}
}

func TestMoonshotStreamDoesNotSendDoneAfterScannerError(t *testing.T) {
	withSSRFBypass(t)
	ctx := t.Context()
	srv := newMoonshotServer(t, func(t *testing.T, _ *http.Request, body map[string]interface{}, w http.ResponseWriter) {
		if body["stream"] != true {
			t.Errorf("stream=%v, want true", body["stream"])
		}
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = io.WriteString(w, "data: "+strings.Repeat("x", 1024*1024+1)+"\n")
	})
	defer srv.Close()

	apiKey := "test-key"
	var sawDone bool
	err := newMoonshotForTest(srv.URL).ChatStreamlyWithSender(
		ctx,
		"kimi-k2.6",
		[]Message{{Role: "user", Content: "ping"}},
		&APIConfig{ApiKey: &apiKey},
		nil,
		nil,
		func(answer, _ *string) error {
			if answer != nil && *answer == "[DONE]" {
				sawDone = true
			}
			return nil
		},
	)
	if err == nil {
		t.Fatal("expected scanner error")
	}
	if sawDone {
		t.Fatal("sender received [DONE] after scanner error")
	}
}

func TestMoonshotListModelsUsesBodylessGet(t *testing.T) {
	withSSRFBypass(t)
	ctx := t.Context()
	srv := newMoonshotServer(t, func(t *testing.T, r *http.Request, _ map[string]interface{}, w http.ResponseWriter) {
		if r.Method != http.MethodGet {
			t.Errorf("method=%s, want GET", r.Method)
		}
		if r.URL.Path != "/models" {
			t.Errorf("path=%s, want /models", r.URL.Path)
		}
		_ = json.NewEncoder(w).Encode(map[string]interface{}{
			"data": []map[string]string{
				{"id": "kimi-k2.6"},
				{"id": " moonshot-v1-8k "},
			},
		})
	})
	defer srv.Close()

	apiKey := "test-key"
	models, err := newMoonshotForTest(srv.URL).ListModels(ctx, &APIConfig{ApiKey: &apiKey})
	if err != nil {
		t.Fatalf("ListModels: %v", err)
	}
	if got := joinModelNames(models, ","); got != "kimi-k2.6,moonshot-v1-8k" {
		t.Errorf("models=%q", got)
	}
}

func TestMoonshotBalanceUsesBodylessGet(t *testing.T) {
	withSSRFBypass(t)
	ctx := t.Context()
	srv := newMoonshotServer(t, func(t *testing.T, r *http.Request, _ map[string]interface{}, w http.ResponseWriter) {
		if r.Method != http.MethodGet {
			t.Errorf("method=%s, want GET", r.Method)
		}
		if r.URL.Path != "/users/me/balance" {
			t.Errorf("path=%s, want /users/me/balance", r.URL.Path)
		}
		_ = json.NewEncoder(w).Encode(map[string]interface{}{
			"code":   0,
			"status": true,
			"data": map[string]float64{
				"available_balance": 49.5,
				"voucher_balance":   46.5,
				"cash_balance":      3,
			},
		})
	})
	defer srv.Close()

	apiKey := "test-key"
	balance, err := newMoonshotForTest(srv.URL).Balance(ctx, &APIConfig{ApiKey: &apiKey})
	if err != nil {
		t.Fatalf("Balance: %v", err)
	}
	if balance["balance"] != 49.5 {
		t.Errorf("balance=%v, want 49.5", balance["balance"])
	}
	if balance["currency"] != "CNY" {
		t.Errorf("currency=%v, want CNY", balance["currency"])
	}
}

func TestMoonshotRejectsMalformedResponses(t *testing.T) {
	withSSRFBypass(t)
	ctx := t.Context()
	apiKey := "test-key"
	tests := []struct {
		name     string
		response map[string]interface{}
		run      func(*MoonshotModel) error
	}{
		{
			name:     "models missing data",
			response: map[string]interface{}{"object": "list"},
			run: func(m *MoonshotModel) error {
				_, err := m.ListModels(ctx, &APIConfig{ApiKey: &apiKey})
				return err
			},
		},
		{
			name: "models empty id",
			response: map[string]interface{}{
				"data": []map[string]string{{"id": ""}},
			},
			run: func(m *MoonshotModel) error {
				_, err := m.ListModels(ctx, &APIConfig{ApiKey: &apiKey})
				return err
			},
		},
		{
			name: "balance missing available balance",
			response: map[string]interface{}{
				"data": map[string]float64{"cash_balance": 3},
			},
			run: func(m *MoonshotModel) error {
				_, err := m.Balance(ctx, &APIConfig{ApiKey: &apiKey})
				return err
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			srv := newMoonshotServer(t, func(t *testing.T, _ *http.Request, _ map[string]interface{}, w http.ResponseWriter) {
				_ = json.NewEncoder(w).Encode(tt.response)
			})
			defer srv.Close()

			if err := tt.run(newMoonshotForTest(srv.URL)); err == nil {
				t.Fatal("expected malformed response error")
			}
		})
	}
}

func TestMoonshotValidatesInputs(t *testing.T) {
	withSSRFBypass(t)
	ctx := t.Context()
	apiKey := "test-key"
	emptyKey := " "
	send := func(*string, *string) error { return nil }

	tests := []struct {
		name string
		run  func() error
		want string
	}{
		{
			name: "chat api key",
			run: func() error {
				_, err := newMoonshotForTest("http://unused").ChatWithMessages(ctx, "kimi-k2.6", []Message{{Role: "user", Content: "x"}}, &APIConfig{ApiKey: &emptyKey}, nil, nil)
				return err
			},
			want: "api key is required",
		},
		{
			name: "chat model",
			run: func() error {
				_, err := newMoonshotForTest("http://unused").ChatWithMessages(ctx, " ", []Message{{Role: "user", Content: "x"}}, &APIConfig{ApiKey: &apiKey}, nil, nil)
				return err
			},
			want: "model name is required",
		},
		{
			name: "stream api key",
			run: func() error {
				return newMoonshotForTest("http://unused").ChatStreamlyWithSender(ctx, "kimi-k2.6", []Message{{Role: "user", Content: "x"}}, nil, nil, nil, send)
			},
			want: "api key is required",
		},
		{
			name: "stream model",
			run: func() error {
				return newMoonshotForTest("http://unused").ChatStreamlyWithSender(ctx, " ", []Message{{Role: "user", Content: "x"}}, &APIConfig{ApiKey: &apiKey}, nil, nil, send)
			},
			want: "model name is required",
		},
		{
			name: "stream sender",
			run: func() error {
				return newMoonshotForTest("http://unused").ChatStreamlyWithSender(ctx, "kimi-k2.6", []Message{{Role: "user", Content: "x"}}, &APIConfig{ApiKey: &apiKey}, nil, nil, nil)
			},
			want: "sender is required",
		},
		{
			name: "models api key",
			run: func() error {
				_, err := newMoonshotForTest("http://unused").ListModels(ctx, &APIConfig{})
				return err
			},
			want: "api key is required",
		},
		{
			name: "balance api key",
			run: func() error {
				_, err := newMoonshotForTest("http://unused").Balance(ctx, &APIConfig{})
				return err
			},
			want: "api key is required",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := tt.run()
			if err == nil || !strings.Contains(err.Error(), tt.want) {
				t.Fatalf("expected %q error, got %v", tt.want, err)
			}
		})
	}
}
