package aifin

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

// Action names are the closed Agent-visible Wind AIFin Market catalog. The
// upstream MCP tool names remain fixed snake_case constants in routing.go.
const (
	ActionGetStockPriceIndicators   = "get-stock-price-indicators"
	ActionGetStockKline             = "get-stock-kline"
	ActionSearchStocks              = "search-stocks"
	ActionGetStockFundamentals      = "get-stock-fundamentals"
	ActionGetFundPriceIndicators    = "get-fund-price-indicators"
	ActionGetFundKline              = "get-fund-kline"
	ActionSearchFunds               = "search-funds"
	ActionGetFundPerformance        = "get-fund-performance"
	ActionGetIndexPriceIndicators   = "get-index-price-indicators"
	ActionGetIndexKline             = "get-index-kline"
	ActionGetCompanyAnnouncements   = "get-company-announcements"
	ActionGetFinancialNews          = "get-financial-news"
	ActionNaturalLanguageGetEDBData = "natural-language-get-edb-data"
)

type actionContract struct {
	name        string
	summary     string
	description string
	input       map[string]any
}

// Definition returns the thirteen-action static catalog supported by the
// pinned OpenConnector code-level input contracts.
func Definition() registry.Definition {
	contracts := []actionContract{
		{
			name:        ActionGetStockPriceIndicators,
			summary:     "Get current stock price indicators",
			description: "Get the current price, trading, valuation, and market-cap indicators for up to 50 stocks.",
			input:       priceIndicatorInputSchema(),
		},
		{
			name:        ActionGetStockKline,
			summary:     "Get historical stock K-lines",
			description: "Get historical stock K-line price and volume data for an explicit date range and period.",
			input:       klineInputSchema(),
		},
		{
			name:        ActionSearchStocks,
			summary:     "Screen stocks",
			description: "Screen stock markets with natural-language financial, valuation, technical, or business criteria.",
			input:       questionInputSchema(),
		},
		{
			name:        ActionGetStockFundamentals,
			summary:     "Get stock fundamentals",
			description: "Get stock financial statement, ratio, valuation, dividend, and market-cap data using a natural-language query.",
			input:       questionInputSchema(),
		},
		{
			name:        ActionGetFundPriceIndicators,
			summary:     "Get current fund price indicators",
			description: "Get the current trading indicators for up to 50 exchange-traded funds or listed open-ended funds.",
			input:       priceIndicatorInputSchema(),
		},
		{
			name:        ActionGetFundKline,
			summary:     "Get historical fund K-lines",
			description: "Get historical ETF or LOF K-line price data for an explicit date range and period.",
			input:       klineInputSchema(),
		},
		{
			name:        ActionSearchFunds,
			summary:     "Screen funds",
			description: "Screen funds with natural-language performance, size, fee, holding, or product criteria.",
			input:       questionInputSchema(),
		},
		{
			name:        ActionGetFundPerformance,
			summary:     "Get fund performance",
			description: "Get fund NAV, returns, rankings, risk-adjusted metrics, ratings, and performance data using a natural-language query.",
			input:       questionInputSchema(),
		},
		{
			name:        ActionGetIndexPriceIndicators,
			summary:     "Get current index price indicators",
			description: "Get the current price and trading indicators for up to 50 indices or sectors.",
			input:       priceIndicatorInputSchema(),
		},
		{
			name:        ActionGetIndexKline,
			summary:     "Get historical index K-lines",
			description: "Get historical index or sector K-line data for an explicit date range and period.",
			input:       klineInputSchema(),
		},
		{
			name:        ActionGetCompanyAnnouncements,
			summary:     "Search company announcements",
			description: "Search listed-company announcements by company, document type, topic, and time range.",
			input:       documentSearchInputSchema(),
		},
		{
			name:        ActionGetFinancialNews,
			summary:     "Search financial news",
			description: "Search financial news by topic, entity, and time range.",
			input:       documentSearchInputSchema(),
		},
		{
			name:        ActionNaturalLanguageGetEDBData,
			summary:     "Search or fetch Wind EDB data",
			description: "Search Wind EDB economic indicators, fetch their time-series values, or do both.",
			input:       economicDataInputSchema(),
		},
	}
	actions := make([]registry.DefinitionAction, 0, len(contracts))
	for _, contract := range contracts {
		actions = append(actions, registry.DefinitionAction{
			Name:         contract.name,
			Summary:      contract.summary,
			Description:  contract.description,
			Safety:       readSafety(),
			InputSchema:  contract.input,
			OutputSchema: toolResultOutputSchema(),
		})
	}
	return registry.Definition{
		Domain:      Domain,
		Version:     1,
		Name:        "Wind AIFin Market",
		Description: "Read stocks, funds, indices, company announcements, financial news, and economic indicators through five fixed Wind MCP services.",
		Actions:     actions,
	}
}

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