package app

import (
	"github.com/voocel/agentcore"
	"github.com/voocel/agentcore/memory"
	"github.com/voocel/ainovel-cli/state"
	"github.com/voocel/ainovel-cli/tools"
)

// BuildCoordinator 组装 Coordinator Agent 及其 SubAgent。
// 返回 Agent 和 AskUserTool（供调用方注入 handler）。
func BuildCoordinator(
	cfg Config,
	store *state.Store,
	model agentcore.ChatModel,
	refs tools.References,
	prompts Prompts,
	styles map[string]string,
) (*agentcore.Agent, *tools.AskUserTool) {
	// 共享工具
	contextTool := tools.NewContextTool(store, refs, cfg.Style)
	readChapter := tools.NewReadChapterTool(store)
	askUser := tools.NewAskUserTool()

	// Architect SubAgent 工具
	architectTools := []agentcore.Tool{
		contextTool,
		tools.NewSaveFoundationTool(store),
	}

	// Writer SubAgent 工具：读写 + 规划 + 一致性检查 + 提交
	writerTools := []agentcore.Tool{
		contextTool,
		readChapter,
		tools.NewPlanChapterTool(store),
		tools.NewDraftChapterTool(store),
		tools.NewCheckConsistencyTool(store),
		tools.NewCommitChapterTool(store),
	}

	// Editor SubAgent 工具：读原文 + 审阅 + 摘要
	editorTools := []agentcore.Tool{
		contextTool,
		readChapter,
		tools.NewSaveReviewTool(store),
		tools.NewSaveArcSummaryTool(store),
		tools.NewSaveVolumeSummaryTool(store),
	}

	architectShort := agentcore.SubAgentConfig{
		Name:         "architect_short",
		Description:  "短篇规划师：为单卷、单冲突、高密度故事生成紧凑设定与扁平大纲",
		Model:        model,
		SystemPrompt: prompts.ArchitectShort,
		Tools:        architectTools,
		MaxTurns:     10,
	}

	architectMid := agentcore.SubAgentConfig{
		Name:         "architect_mid",
		Description:  "中篇规划师：为多阶段但篇幅受控的故事生成可推进的设定与阶段化大纲",
		Model:        model,
		SystemPrompt: prompts.ArchitectMid,
		Tools:        architectTools,
		MaxTurns:     12,
	}

	architectLong := agentcore.SubAgentConfig{
		Name:         "architect_long",
		Description:  "长篇规划师：为连载型、可持续升级的故事生成分层设定与卷弧大纲",
		Model:        model,
		SystemPrompt: prompts.ArchitectLong,
		Tools:        architectTools,
		MaxTurns:     14,
	}

	// 动态拼接风格指令到 Writer prompt
	writerPrompt := prompts.Writer
	if style, ok := styles[cfg.Style]; ok {
		writerPrompt += "\n\n" + style
	}

	writer := agentcore.SubAgentConfig{
		Name:             "writer",
		Description:      "创作者：自主完成一章的构思、写作、自审和提交",
		Model:            model,
		SystemPrompt:     writerPrompt,
		Tools:            writerTools,
		MaxTurns:         20,
		TransformContext: memory.NewCompaction(memory.CompactionConfig{
			Model:            model,
			ContextWindow:    cfg.ContextWindow,
			ReserveTokens:    16384,
			KeepRecentTokens: 20000,
		}),
		ConvertToLLM: memory.CompactionConvertToLLM,
	}

	editor := agentcore.SubAgentConfig{
		Name:         "editor",
		Description:  "审阅者：阅读原文，从结构和审美两个层面发现问题",
		Model:        model,
		SystemPrompt: prompts.Editor,
		Tools:        editorTools,
		MaxTurns:     10,
	}

	subagentTool := agentcore.NewSubAgentTool(architectShort, architectMid, architectLong, writer, editor)

	agent := agentcore.NewAgent(
		agentcore.WithModel(model),
		agentcore.WithSystemPrompt(prompts.Coordinator),
		agentcore.WithTools(subagentTool, contextTool, askUser),
		agentcore.WithMaxTurns(60),
		agentcore.WithContextPipeline(
			memory.NewCompaction(memory.CompactionConfig{
				Model:            model,
				ContextWindow:    cfg.ContextWindow,
				ReserveTokens:    32000,
				KeepRecentTokens: 30000,
			}),
			memory.CompactionConvertToLLM,
		),
	)
	return agent, askUser
}
