package hotnews

import (
	"github.com/tidbcloud/AgentTeam/internal/extension/registry"
	"github.com/tidbcloud/AgentTeam/internal/extension/spec"
)

// Action name constants (lowercase kebab-case per the registry catalog
// contract).
const (
	ActionGetTrending   = "get-trending"
	ActionListPlatforms = "list-platforms"
)

// platform is one catalog entry in the closed platform directory: the newsnow
// source ID accepted by get-trending, the human-readable name served by
// list-platforms, and the expected domain enforced for non-empty item links.
type platform struct {
	id             string
	name           string
	expectedDomain string
}

// platforms is the closed get-trending enum in stable catalog order.
var platforms = []platform{
	{id: "baidu", name: "百度热搜", expectedDomain: "baidu.com"},
	{id: "bilibili-hot-search", name: "bilibili 热搜", expectedDomain: "bilibili.com"},
	{id: "cls-hot", name: "财联社热门", expectedDomain: "cls.cn"},
	{id: "douyin", name: "抖音", expectedDomain: "douyin.com"},
	{id: "douban", name: "豆瓣", expectedDomain: "douban.com"},
	{id: "ifeng", name: "凤凰网", expectedDomain: "ifeng.com"},
	{id: "producthunt", name: "Product Hunt", expectedDomain: "producthunt.com"},
	{id: "tencent-hot", name: "腾讯新闻", expectedDomain: "qq.com"},
	{id: "tieba", name: "百度贴吧", expectedDomain: "baidu.com"},
	{id: "wallstreetcn-hot", name: "华尔街见闻", expectedDomain: "wallstreetcn.com"},
	{id: "weibo", name: "微博", expectedDomain: "weibo.com"},
}

// platformIDs returns the closed enum values for the input schema.
func platformIDs() []any {
	ids := make([]any, 0, len(platforms))
	for _, entry := range platforms {
		ids = append(ids, entry.id)
	}
	return ids
}

// platformByID returns the complete catalog record so schema generation,
// list-platforms, the handler guard, and link-domain validation all share one
// authority.
func platformByID(id string) (platform, bool) {
	for _, entry := range platforms {
		if entry.id == id {
			return entry, true
		}
	}
	return platform{}, false
}

// Definition returns the Agent-visible HotNews catalog: one wire read and
// one static directory Action, both with read safety.
func Definition() registry.Definition {
	return registry.Definition{
		Domain:      Domain,
		Version:     1,
		Name:        "HotNews",
		Description: "Read trending-topic feeds from Weibo, Baidu, Bilibili, Douyin, Douban, Ifeng, Product Hunt, Tencent News, Tieba, CLS, and Wallstreetcn.",
		Actions: []registry.DefinitionAction{
			{
				Name:         ActionGetTrending,
				Summary:      "Get one platform's trending feed",
				Description:  "Get a supported platform's current trending-topic feed with its status, source ID, update time, and ranked items.",
				Safety:       readSafety(),
				InputSchema:  getTrendingInputSchema(),
				OutputSchema: trendingOutputSchema(),
				Example: map[string]any{
					"platform_id": "weibo",
				},
			},
			{
				Name:        ActionListPlatforms,
				Summary:     "List the supported trending platforms",
				Description: "List every supported source ID accepted by get-trending with its display name. Served locally; no upstream call.",
				Safety:      readSafety(),
				InputSchema: objectSchema(
					nil,
					nil,
					"The directory read takes no input.",
				),
				OutputSchema: listPlatformsOutputSchema(),
				Example:      map[string]any{},
			},
		},
	}
}

func readSafety() spec.Safety {
	return spec.Safety{Class: "read", Idempotent: true, Destructive: false}
}
