package mcp

import (
	mcpsdk "github.com/modelcontextprotocol/go-sdk/mcp"

	"github.com/yan1h/agent-coding-workflow/internal/changerequest"
	"github.com/yan1h/agent-coding-workflow/internal/chat"
	"github.com/yan1h/agent-coding-workflow/internal/codeindex"
	"github.com/yan1h/agent-coding-workflow/internal/project"
	"github.com/yan1h/agent-coding-workflow/internal/projectmemory"
	"github.com/yan1h/agent-coding-workflow/internal/workspace"
)

// ServerDeps 是装配 mcp.Server 所需的下游 service 集合。
type ServerDeps struct {
	Caller        *CallerResolver
	Projects      project.ProjectService
	Reqs          project.RequirementService
	Issues        project.IssueService
	Artifacts     project.ArtifactService
	Workspaces    workspace.WorkspaceService
	Worktrees     workspace.WorktreeService
	GitOps        workspace.GitOpsService
	ChangeReqs    changerequest.Service
	Templates     chat.TemplateService
	Messages      chat.MessageService
	Conversations chat.ConversationService
	Endpoints     chat.EndpointService
	ACP           ACPBridge             // acp 服务桥接（见 tools_acp.go / acp.NewMCPBridge）
	Runs          RunService            // *run.Service（见 tools_run.go）
	Execs         ExecService           // *run.ExecService — 一次性 exec（run_command / run_tests）
	Orchestrator  OrchestratorBridge    // 编排器桥接（见 tools_orchestrator.go）；可为 nil（禁用时）
	CodeIndex     codeindex.Service     // 代码索引 RAG（见 tools_codeindex.go）；可为 nil（禁用时）
	Memory        projectmemory.Service // 项目记忆（见 tools_memory.go）；可为 nil（禁用时）
}

// NewMCPServer 装配 SDK Server 实例并注册所有工具/prompts/resources。
func NewMCPServer(deps ServerDeps) *mcpsdk.Server {
	srv := mcpsdk.NewServer(&mcpsdk.Implementation{
		Name:    "agent-coding-workflow",
		Version: "0.1.0",
	}, nil)

	// Phase F: PM 工具（projects / requirements / issues / artifacts）
	registerPMTools(srv, deps)
	// Phase G: chat / prompts / resources
	registerChatTools(srv, deps)
	registerPrompts(srv, deps)
	registerResources(srv, deps)
	// Phase H: workspace / worktree / git、ACP、run profiles
	registerWorkspaceTools(srv, deps)
	registerChangeRequestTools(srv, deps)
	registerACPTools(srv, deps)
	registerRunTools(srv, deps)
	registerOrchestratorTools(srv, deps)
	registerCodeIndexTools(srv, deps)
	registerMemoryTools(srv, deps)

	return srv
}
