package promptcompiler

import (
	"strings"

	"github.com/tidbcloud/AgentTeam/desktop/internal/types"
)

const (
	// Skill source precedence is a compiler policy, not a runtime materializer
	// decision.
	SkillSourcePriorityProfile = 100
	SkillSourcePriorityBuiltin = 150
	SkillSourcePriorityManaged = 200
)

// CompileAgentTeamSkillPlan returns the single ordered, budgeted skill decision used
// by both prompt compilation and runtime materialization. The second argument
// is a concrete runtime tool identifier, not pre-rendered guidance prose.
func CompileAgentTeamSkillPlan(toolName types.ToolNamer, memorySearchTool string) SkillPlan {
	data := agentteamSkillDocData(toolName)
	reference := AgentTeamReferenceTemplateData(data)
	plan := SkillPlan{
		Core:          renderRegisteredPromptText("platform.agentteam.core", newAgentTeamCorePromptInput(data)),
		Reference:     renderRegisteredPromptText("platform.agentteam.reference", newLoopReferencePromptInput(reference)),
		CoreData:      &data,
		ReferenceData: &reference,
		BudgetTokens:  DefaultMaxBlockTokens,
		OrderedSlots: []SkillPlanSlot{
			{Name: "agentteam.reference", Priority: 20, Origin: "agentteam", PromptVisible: true},
			{Name: "agentteam.core", Priority: 40, Origin: "agentteam", PromptVisible: true},
		},
	}
	if memorySearchTool = strings.TrimSpace(memorySearchTool); memorySearchTool != "" {
		plan.MemorySearchData = &MemorySearchGuidanceTemplateData{SearchTool: memorySearchTool}
	}
	return plan
}

// AgentTeamSkillDocParts returns the message-tool prompt projection without
// constructing a runtime-bound MCP handler.
func AgentTeamSkillDocParts(toolName types.ToolNamer) types.SkillDocParts {
	plan := CompileAgentTeamSkillPlan(toolName, "")
	return types.SkillDocParts{Core: plan.Core, Reference: plan.Reference}
}

// MemorySearchGuidance renders the registered AgentTeam Memory capability guidance
// for tool-description surfaces that cannot carry a full SkillPlan.
func MemorySearchGuidance(toolName string) string {
	toolName = strings.TrimSpace(toolName)
	if toolName == "" {
		return ""
	}
	return renderRegisteredPromptText("platform.agentteam.memory_search", newMemorySearchGuidancePromptInput(MemorySearchGuidanceTemplateData{SearchTool: toolName}))
}

// AgentTeamMemoryResumeReminder returns the stable resume reminder used by AgentTeam
// message prompts.
func AgentTeamMemoryResumeReminder() string {
	return "\n\n" + renderRegisteredPromptText("runtime.memory_resume_reminder", newEmptyPromptInput(PromptInputRecovery))
}

func agentteamSkillDocData(toolName types.ToolNamer) AgentTeamCoreTemplateData {
	return AgentTeamCoreTemplateData{
		AddReactionTool: toolName("message_reaction_set"), CreateTasksTool: "agentteam exec task create", GetReactionsTool: toolName("message_reaction_list"),
		ListWorkspaceTool: "agentteam exec workspace list", ListTasksTool: "agentteam exec task list", ReadHistoryTool: toolName("message_history_read"),
		RemoveReactionTool: toolName("message_reaction_set"), ReviewAddTool: "agentteam exec review add", ReviewDeleteTool: "agentteam exec review delete", ReviewListTool: "agentteam exec review list",
		SearchMessagesTool: toolName("message_search"), SearchTasksTool: "agentteam exec task list", SendMessageTool: toolName("message_send"),
		TaskAddCheckpointTool: "agentteam exec task checkpoint", TaskGetTool: "agentteam exec task get", TaskUpdateTool: "agentteam exec task update",
		ScheduledMessageCreateTool: "agentteam exec scheduled-message create", ScheduledMessageDeleteTool: "agentteam exec scheduled-message delete", ScheduledMessageGetTool: "agentteam exec scheduled-message get",
		ScheduledMessageListTool: "agentteam exec scheduled-message list", ScheduledMessagePauseTool: "agentteam exec scheduled-message pause", ScheduledMessageResumeTool: "agentteam exec scheduled-message resume",
		ScheduledMessageRunsTool: "agentteam exec scheduled-message runs", ScheduledMessageUpdateTool: "agentteam exec scheduled-message update", ScheduledMessageValidateTool: "agentteam exec scheduled-message validate",
		ViewFileTool: toolName("message_attachment_view"),
	}
}
