package gateway

import (
	"encoding/json"

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

// IngressModelID returns the model identifier exposed to ingress clients.
func IngressModelID(m model.ModelEntry) string {
	return m.IngressID()
}

func modelEntryMatches(m model.ModelEntry, name string) bool {
	return m.MatchesIngressName(name)
}

func (s *Service) resolveUpstreamModelID(providerID uint, ingressModel string) string {
	if ingressModel == "" {
		return ""
	}
	for _, m := range s.cache.GetModels(providerID) {
		if modelEntryMatches(m, ingressModel) {
			return m.ModelID
		}
	}
	return ingressModel
}

func rewriteModelInBody(body []byte, upstreamModel string) ([]byte, error) {
	if upstreamModel == "" || len(body) == 0 {
		return body, nil
	}
	var payload map[string]any
	if err := json.Unmarshal(body, &payload); err != nil {
		return body, nil
	}
	current, _ := payload["model"].(string)
	if current == "" || current == upstreamModel {
		return body, nil
	}
	payload["model"] = upstreamModel
	return json.Marshal(payload)
}

func (s *Service) rewriteBodyForProvider(providerID uint, body []byte) ([]byte, error) {
	ingressModel := extractModelName(body)
	if ingressModel == "" {
		return body, nil
	}
	upstream := s.resolveUpstreamModelID(providerID, ingressModel)
	return rewriteModelInBody(body, upstream)
}
