package qichacha

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

// Action name constants for the Qichacha catalog.
const (
	ActionListCompanyShareholders          = "list-company-shareholders"
	ActionListCompanyHistoricalInvestments = "list-company-historical-investments"
)

// Definition returns the Agent-visible Qichacha catalog.
func Definition() registry.Definition {
	return registry.Definition{
		Domain:      Domain,
		Version:     1,
		Name:        "Qichacha",
		Description: "Query Qichacha business-registration data: company shareholders and historical outbound investments with exit details.",
		Actions: []registry.DefinitionAction{
			{
				Name:        ActionListCompanyShareholders,
				Summary:     "List a company's shareholders",
				Description: "List a company's shareholders and subscribed contribution details from Qichacha business registration data.",
				Safety:      readSafety(),
				InputSchema: searchInputSchema(),
				OutputSchema: objectSchema(mapsMerge(
					map[string]any{
						"shareholders": map[string]any{
							"type":        "array",
							"description": "The registered shareholder records.",
							"items":       shareholderSchema(),
						},
					},
					paginationProperties(),
				), nil),
			},
			{
				Name:        ActionListCompanyHistoricalInvestments,
				Summary:     "List a company's historical outbound investments",
				Description: "List a company's historical outbound investments and exit details from Qichacha.",
				Safety:      readSafety(),
				InputSchema: searchInputSchema(),
				OutputSchema: objectSchema(mapsMerge(
					map[string]any{
						"historicalInvestments": map[string]any{
							"type":        "array",
							"description": "The historical outbound investment records.",
							"items":       historicalInvestmentSchema(),
						},
					},
					paginationProperties(),
				), nil),
			},
		},
	}
}

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

func mapsMerge(base map[string]any, extra map[string]any) map[string]any {
	merged := make(map[string]any, len(base)+len(extra))
	for name, schema := range base {
		merged[name] = schema
	}
	for name, schema := range extra {
		merged[name] = schema
	}
	return merged
}
