#include "DruidsMCPBridgeEditorSettings.h"

#include "DruidsMCPBridge.h"
#include "HAL/PlatformApplicationMisc.h"

UDruidsMCPBridgeEditorSettings::UDruidsMCPBridgeEditorSettings()
{
	RefreshFromEndpoint(GetDefaultEndpointUrl(), false);
}

void UDruidsMCPBridgeEditorSettings::RefreshFromEndpoint(const FString& InEndpointUrl, bool bInServerRunning)
{
	const FString EndpointUrl = InEndpointUrl.IsEmpty() ? GetDefaultEndpointUrl() : InEndpointUrl;

	ServerEndpoint = EndpointUrl;
	ServerStatus = bInServerRunning
		? FString::Printf(TEXT("Running at %s"), *EndpointUrl)
		: FString::Printf(TEXT("Not running. Expected endpoint: %s"), *EndpointUrl);

	CodexConfigSnippet = FString::Printf(
		TEXT("[mcp_servers.druids-unreal]\nurl = \"%s\"\nenabled = true"),
		*EndpointUrl);

	CodexInstallCommand = FString::Printf(
		TEXT("codex mcp add druids-unreal --url %s"),
		*EndpointUrl);

	ClaudeCodeInstallCommand = FString::Printf(
		TEXT("claude mcp add --transport http druids-unreal %s"),
		*EndpointUrl);

	GeminiInstallCommand = FString::Printf(
		TEXT("gemini mcp add --scope user --transport http druids-unreal %s"),
		*EndpointUrl);

	GeminiMcpJson = FString::Printf(
		TEXT("{\n  \"mcpServers\": {\n    \"druids-unreal\": {\n      \"httpUrl\": \"%s\",\n      \"trust\": false\n    }\n  }\n}"),
		*EndpointUrl);

	AntigravityMcpJson = FString::Printf(
		TEXT("{\n  \"mcpServers\": {\n    \"druids-unreal\": {\n      \"httpUrl\": \"%s\"\n    }\n  }\n}"),
		*EndpointUrl);

	CursorMcpJson = FString::Printf(
		TEXT("{\n  \"mcpServers\": {\n    \"druids-unreal\": {\n      \"url\": \"%s\"\n    }\n  }\n}"),
		*EndpointUrl);

	JetBrainsMcpJson = FString::Printf(
		TEXT("{\n  \"mcpServers\": {\n    \"druids-unreal\": {\n      \"transport\": \"http\",\n      \"url\": \"%s\"\n    }\n  }\n}"),
		*EndpointUrl);

	SetupNotes = TEXT("Use Tools > Druids MCP Bridge > Connect AI Assistant... for guided setup. Or add the snippet/run the command for your AI client, then restart that client so it reloads MCP servers. MCP does not currently provide universal OS-level discovery for local Unreal plugins.");
}

void UDruidsMCPBridgeEditorSettings::RefreshSetupSnippets()
{
	RefreshFromEndpoint(ServerEndpoint, ServerStatus.StartsWith(TEXT("Running")));
}

void UDruidsMCPBridgeEditorSettings::CopyCodexConfigSnippet() const
{
	CopyTextToClipboard(CodexConfigSnippet);
}

void UDruidsMCPBridgeEditorSettings::CopyCodexInstallCommand() const
{
	CopyTextToClipboard(CodexInstallCommand);
}

void UDruidsMCPBridgeEditorSettings::CopyClaudeCodeInstallCommand() const
{
	CopyTextToClipboard(ClaudeCodeInstallCommand);
}

void UDruidsMCPBridgeEditorSettings::CopyGeminiInstallCommand() const
{
	CopyTextToClipboard(GeminiInstallCommand);
}

void UDruidsMCPBridgeEditorSettings::CopyGeminiMcpJson() const
{
	CopyTextToClipboard(GeminiMcpJson);
}

void UDruidsMCPBridgeEditorSettings::CopyAntigravityMcpJson() const
{
	CopyTextToClipboard(AntigravityMcpJson);
}

void UDruidsMCPBridgeEditorSettings::CopyCursorMcpJson() const
{
	CopyTextToClipboard(CursorMcpJson);
}

void UDruidsMCPBridgeEditorSettings::CopyJetBrainsMcpJson() const
{
	CopyTextToClipboard(JetBrainsMcpJson);
}

FString UDruidsMCPBridgeEditorSettings::GetDefaultEndpointUrl()
{
	return TEXT("http://127.0.0.1:8765/mcp");
}

void UDruidsMCPBridgeEditorSettings::CopyTextToClipboard(const FString& Text)
{
	FPlatformApplicationMisc::ClipboardCopy(*Text);
	UE_LOG(LogDruidsMCPBridge, Display, TEXT("Copied Druids MCP Bridge setup text to clipboard"));
}
