package handlers

// Server-initiated initialize call: types per spec §5.4. Supervisor sends this
// immediately after spawn. clientCapabilities.terminal=false (MVP no terminal).

type InitializeParams struct {
	ProtocolVersion    int                `json:"protocolVersion"`
	ClientCapabilities ClientCapabilities `json:"clientCapabilities"`
	ClientInfo         ClientInfo         `json:"clientInfo"`
}

type ClientCapabilities struct {
	Fs       FsCapability `json:"fs"`
	Terminal bool         `json:"terminal"`
}

type FsCapability struct {
	ReadTextFile  bool `json:"readTextFile"`
	WriteTextFile bool `json:"writeTextFile"`
}

type ClientInfo struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

type InitializeResult struct {
	ProtocolVersion   int            `json:"protocolVersion"`
	AgentCapabilities map[string]any `json:"agentCapabilities,omitempty"`
	AgentInfo         map[string]any `json:"agentInfo,omitempty"`
	AuthMethods       []any          `json:"authMethods,omitempty"`
}

// McpCapabilities 从 agentCapabilities.mcpCapabilities 解析 http/sse 支持声明
// （ACP v1：均默认 false；stdio 是基线无需声明）。字段缺失或形状异常按 false 处理。
func (r InitializeResult) McpCapabilities() (httpOK, sseOK bool) {
	caps, ok := r.AgentCapabilities["mcpCapabilities"].(map[string]any)
	if !ok {
		return false, false
	}
	httpOK, _ = caps["http"].(bool)
	sseOK, _ = caps["sse"].(bool)
	return httpOK, sseOK
}

// BuildInitializeParams returns standard initialize params.
func BuildInitializeParams(version string) InitializeParams {
	return InitializeParams{
		ProtocolVersion: 1,
		ClientCapabilities: ClientCapabilities{
			Fs:       FsCapability{ReadTextFile: true, WriteTextFile: true},
			Terminal: false,
		},
		ClientInfo: ClientInfo{Name: "agent-coding-workflow", Version: version},
	}
}
