package prediction

import (
	"context"
	"fmt"
	"strings"
	"time"

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

func splitOwnerName(ingressID, providerName string) (owner, name string) {
	return model.SplitIngressOwnerName(ingressID, providerName)
}

func modelDescription(m model.ModelEntry) string {
	if m.DisplayName != "" {
		return m.DisplayName
	}
	switch m.WorkflowType {
	case model.WorkflowImg2Img:
		return "Image-to-image workflow"
	default:
		return "Text-to-image workflow"
	}
}

// ListPublicModels returns Replicate-compatible model objects visible to the ingress key.
func (s *Service) ListPublicModels(ctx context.Context, ingress *model.IngressKey, baseURL string) ([]map[string]any, error) {
	_ = ctx
	allowedProviders := cache.ParseJSONList(ingress.AllowedProvidersJSON)
	allowedModels := cache.ParseJSONList(ingress.AllowedModelsJSON)
	baseURL = strings.TrimRight(baseURL, "/")

	var results []map[string]any
	for _, p := range s.cache.ListProviders() {
		if p.Category != model.CategoryImage || !p.Enabled {
			continue
		}
		if !cache.MatchesList(allowedProviders, p.Name) && !cache.MatchesList(allowedProviders, string(p.Type)) && !cache.MatchesList(allowedProviders, "*") {
			continue
		}
		for _, m := range s.cache.GetModels(p.ID) {
			if !m.Enabled || m.VersionHash == "" {
				continue
			}
			ingressID := m.IngressID()
			if !cache.MatchesList(allowedModels, ingressID) && !cache.MatchesList(allowedModels, m.ModelID) && !cache.MatchesList(allowedModels, m.VersionHash) && !cache.MatchesList(allowedModels, "*") {
				continue
			}
			owner, name := splitOwnerName(ingressID, p.Name)
			desc := modelDescription(m)
			createdAt := m.CreatedAt
			if createdAt.IsZero() {
				createdAt = time.Now()
			}
			results = append(results, map[string]any{
				"url":         fmt.Sprintf("%s/v1/models/%s/%s", baseURL, owner, name),
				"owner":       owner,
				"name":        name,
				"description": desc,
				"visibility":  "public",
				"latest_version": map[string]any{
					"id":             ingressID,
					"created_at":     createdAt.UTC().Format(time.RFC3339),
					"cog_version":    "",
					"openapi_schema": OpenAPISchemaForWorkflow(m.WorkflowType, name, desc),
				},
			})
		}
	}
	return results, nil
}
