package gateway

import (
	"encoding/json"
	"fmt"
	"strings"

	"github.com/rose_cat707/luminary/internal/provider"
)

const maxLogBodyBytes = 16384

func truncateLogBody(b []byte) string {
	if len(b) == 0 {
		return ""
	}
	if len(b) <= maxLogBodyBytes {
		return string(b)
	}
	return string(b[:maxLogBodyBytes]) + "\n...(truncated)"
}

func extractModelName(body []byte) string {
	if len(body) == 0 {
		return ""
	}
	var payload struct {
		Model string `json:"model"`
	}
	if err := json.Unmarshal(body, &payload); err != nil {
		return ""
	}
	return payload.Model
}

func describeForwardError(err error, resp *provider.ChatResponse) string {
	if err != nil {
		return err.Error()
	}
	if resp == nil {
		return "unknown error"
	}
	if resp.StatusCode < 400 {
		return ""
	}
	if msg := extractAPIErrorMessage(resp.Body); msg != "" {
		return fmt.Sprintf("status %d: %s", resp.StatusCode, msg)
	}
	if len(resp.Body) > 0 {
		return fmt.Sprintf("status %d: %s", resp.StatusCode, truncateLogBody(resp.Body))
	}
	return fmt.Sprintf("status %d", resp.StatusCode)
}

func extractAPIErrorMessage(body []byte) string {
	if len(body) == 0 {
		return ""
	}
	var openAI struct {
		Error struct {
			Message string `json:"message"`
			Type    string `json:"type"`
		} `json:"error"`
	}
	if err := json.Unmarshal(body, &openAI); err == nil {
		if openAI.Error.Message != "" {
			if openAI.Error.Type != "" {
				return openAI.Error.Type + ": " + openAI.Error.Message
			}
			return openAI.Error.Message
		}
	}
	var simple struct {
		Message string `json:"message"`
		Error   string `json:"error"`
	}
	if err := json.Unmarshal(body, &simple); err == nil {
		if simple.Message != "" {
			return simple.Message
		}
		if simple.Error != "" {
			return simple.Error
		}
	}
	s := strings.TrimSpace(string(body))
	if len(s) > 240 {
		return s[:240] + "..."
	}
	return s
}
