package dingtalkbot

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

// Action name constants for the DingTalk Bot catalog.
const (
	ActionSendTextMessage       = "send-text-message"
	ActionSendLinkMessage       = "send-link-message"
	ActionSendMarkdownMessage   = "send-markdown-message"
	ActionSendActionCardMessage = "send-action-card-message"
	ActionSendFeedCardMessage   = "send-feed-card-message"
)

// Definition returns the Agent-visible DingTalk Bot catalog.
func Definition() registry.Definition {
	return registry.Definition{
		Domain:      Domain,
		Version:     1,
		Name:        "DingTalk Bot",
		Description: "Send text, link, markdown, actionCard, and feedCard messages through the DingTalk custom bot webhook.",
		Actions: []registry.DefinitionAction{
			{
				Name:         ActionSendTextMessage,
				Summary:      "Send a DingTalk text message",
				Description:  "Send a text message through the DingTalk custom bot webhook.",
				Safety:       sendSafety(),
				InputSchema:  sendTextInputSchema(),
				OutputSchema: sendResultSchema(),
				Example: map[string]any{
					"content": "Deploy finished",
				},
			},
			{
				Name:         ActionSendLinkMessage,
				Summary:      "Send a DingTalk link message",
				Description:  "Send a link message through the DingTalk custom bot webhook.",
				Safety:       sendSafety(),
				InputSchema:  sendLinkInputSchema(),
				OutputSchema: sendResultSchema(),
				Example: map[string]any{
					"title":      "Release notes",
					"text":       "v1.2.3 is live",
					"messageUrl": "https://example.com/release",
				},
			},
			{
				Name:         ActionSendMarkdownMessage,
				Summary:      "Send a DingTalk markdown message",
				Description:  "Send a markdown message through the DingTalk custom bot webhook.",
				Safety:       sendSafety(),
				InputSchema:  sendMarkdownInputSchema(),
				OutputSchema: sendResultSchema(),
				Example: map[string]any{
					"title": "Report",
					"text":  "## Build passed",
				},
			},
			{
				Name:         ActionSendActionCardMessage,
				Summary:      "Send a DingTalk actionCard message",
				Description:  "Send an actionCard message through the DingTalk custom bot webhook.",
				Safety:       sendSafety(),
				InputSchema:  sendActionCardInputSchema(),
				OutputSchema: sendResultSchema(),
				Example: map[string]any{
					"cardMode":    "single",
					"title":       "Approval",
					"text":        "Approve the release?",
					"singleTitle": "Open",
					"singleUrl":   "https://example.com/approval",
				},
			},
			{
				Name:         ActionSendFeedCardMessage,
				Summary:      "Send a DingTalk feedCard message",
				Description:  "Send a feedCard message through the DingTalk custom bot webhook.",
				Safety:       sendSafety(),
				InputSchema:  sendFeedCardInputSchema(),
				OutputSchema: sendResultSchema(),
				Example: map[string]any{
					"links": []any{map[string]any{
						"title":      "Merge",
						"messageUrl": "https://example.com/pr/1",
						"picUrl":     "https://example.com/pr.png",
					}},
				},
			},
		},
	}
}

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