#pragma once

#include "CoreMinimal.h"
#include "Containers/Ticker.h"
#include "HAL/IConsoleManager.h"
#include "Modules/ModuleManager.h"

DECLARE_LOG_CATEGORY_EXTERN(LogDruidsMCPBridge, Log, All);

class FDruidsMCPBridgeServer;
class UDruidsMCPBridgeEditorSettings;

class DRUIDSMCPBRIDGE_API FDruidsMCPBridgeModule : public IModuleInterface
{
public:
	virtual void StartupModule() override;
	virtual void ShutdownModule() override;

private:
	void RegisterMenus();
	void RegisterEditorSettings();
	void UnregisterEditorSettings();
	void RefreshEditorSettings();
	void ScheduleAutoStart();
	bool HandleAutoStartTick(float DeltaTime);
	bool ShouldAutoStart() const;
	void StartBridge();
	void StopBridge();
	void ToggleBridge();
	void OpenAISetupWizard();
	bool IsBridgeRunning() const;
	FString GetCurrentEndpointUrl() const;
	FText GetToggleBridgeLabel() const;
	FText GetBridgeStatusLabel() const;

	TSharedPtr<FDruidsMCPBridgeServer> BridgeServer;
	FTSTicker::FDelegateHandle AutoStartTickHandle;
	UDruidsMCPBridgeEditorSettings* EditorSettings = nullptr;
	bool bShutdownStarted = false;
	bool bEditorSettingsRegistered = false;
	TUniquePtr<FAutoConsoleCommand> StartCommand;
	TUniquePtr<FAutoConsoleCommand> StopCommand;
	TUniquePtr<FAutoConsoleCommand> StatusCommand;
};
