package notion

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

// Action name constants for the Notion catalog. The registry requires
// lowercase kebab-case action names, so the upstream snake_case names are
// dash-joined; input and output JSON property names keep the upstream casing.
const (
	ActionSearch               = "search"
	ActionGetPage              = "get-page"
	ActionCreatePage           = "create-page"
	ActionUpdatePage           = "update-page"
	ActionAppendBlock          = "append-block"
	ActionRetrievePageMarkdown = "retrieve-page-markdown"
	ActionListUsers            = "list-users"
)

// Definition returns the Agent-visible Notion catalog.
func Definition() registry.Definition {
	return registry.Definition{
		Domain:      Domain,
		Version:     1,
		Name:        "Notion",
		Description: "Manage Notion pages, blocks, and workspace users with the connected account.",
		Actions: []registry.DefinitionAction{
			{
				Name:        ActionSearch,
				Summary:     "Search Notion pages and data sources",
				Description: "Search Notion pages and data sources with optional filter, sort, and pagination controls.",
				Safety:      readSafety(),
				InputSchema: searchInputSchema(),
				OutputSchema: listOutputSchema(map[string]any{
					"anyOf": []any{pageOutputSchema(), dataSourceOutputSchema()},
				}, "Search results returned by Notion."),
				Example: map[string]any{"query": "onboarding", "pageSize": 20},
			},
			{
				Name:        ActionGetPage,
				Summary:     "Get a Notion page with its child blocks",
				Description: "Get a Notion page together with its first-level child blocks. This is an aggregate helper over page retrieval plus block-children listing.",
				Safety:      readSafety(),
				InputSchema: objectSchema(map[string]any{
					"pageId": nonEmptyStringSchema("The page ID to retrieve."),
				}, []string{"pageId"}),
				OutputSchema: describedObjectSchema("Page with child block list.", map[string]any{
					"page":           pageOutputSchema(),
					"block_children": listOutputSchema(blockOutputSchema(), "First-level child blocks."),
				}, []string{"page", "block_children"}),
				Example: map[string]any{"pageId": "1f48b1c8-f5a8-4b23-9a3e-da3d6c7f4e2a"},
			},
			{
				Name:         ActionCreatePage,
				Summary:      "Create a Notion page",
				Description:  "Create a Notion page under a parent page, data source, or workspace-level private area.",
				Safety:       writeSafety(),
				InputSchema:  createPageInputSchema(),
				OutputSchema: pageOutputSchema(),
				Example: map[string]any{
					"parentId": "1f48b1c8-f5a8-4b23-9a3e-da3d6c7f4e2a",
					"title":    "AgentTeam integrations",
				},
			},
			{
				Name:         ActionUpdatePage,
				Summary:      "Update a Notion page",
				Description:  "Update a Notion page's properties, title, icon, cover, trash status, or locked state.",
				Safety:       writeSafety(),
				InputSchema:  updatePageInputSchema(),
				OutputSchema: pageOutputSchema(),
				Example:      map[string]any{"pageId": "1f48b1c8-f5a8-4b23-9a3e-da3d6c7f4e2a", "title": "Renamed"},
			},
			{
				Name:        ActionAppendBlock,
				Summary:     "Append a paragraph block to a Notion page",
				Description: "Append a single paragraph block to a Notion page.",
				Safety:      writeSafety(),
				InputSchema: objectSchema(map[string]any{
					"pageId": nonEmptyStringSchema("The page ID to append to."),
					"text":   nonEmptyStringSchema("Paragraph text content."),
				}, []string{"pageId", "text"}),
				OutputSchema: listOutputSchema(
					blockOutputSchema(), "Appended block children response."),
				Example: map[string]any{
					"pageId": "1f48b1c8-f5a8-4b23-9a3e-da3d6c7f4e2a",
					"text":   "Ported from OpenConnector.",
				},
			},
			{
				Name:         ActionRetrievePageMarkdown,
				Summary:      "Retrieve a Notion page as Markdown",
				Description:  "Retrieve a Notion page or block subtree rendered as enhanced Markdown.",
				Safety:       readSafety(),
				InputSchema:  retrievePageMarkdownInputSchema(),
				OutputSchema: notionRecordSchema("A Notion API object."),
				Example:      map[string]any{"pageId": "1f48b1c8-f5a8-4b23-9a3e-da3d6c7f4e2a"},
			},
			{
				Name:        ActionListUsers,
				Summary:     "List Notion workspace users",
				Description: "List users in the Notion workspace with pagination.",
				Safety:      readSafety(),
				InputSchema: listUsersInputSchema(),
				OutputSchema: listOutputSchema(
					userOutputSchema(), "Workspace users returned by Notion."),
				Example: map[string]any{"pageSize": 100},
			},
		},
	}
}

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

func writeSafety() spec.Safety {
	return spec.Safety{Class: "mutation", Idempotent: false, Destructive: false}
}
