#pragma once

// Clean-room implementation for HyperTwist algorithm-language subsystem
// Implemented from governance docs only (Phase 0R-D, Phase 1R, Phase 5R-A)
// Date: 2026-05-15
// Bound 4 - versioned structured interchange and URL/share helpers

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "HyperTwistAlgorithm/HyperTwistAlgorithmTypes.h"
#include "HyperTwistAlgorithmShare.generated.h"

USTRUCT(BlueprintType)
struct FHyperTwistAlgorithmShareDocument
{
	GENERATED_BODY()

	// Recursive algorithm trees remain C++ carrier fields, mirroring the
	// underlying owned AST surface rather than forcing a second reflected shape.
	FHyperTwistAlgorithmSequence MainSequence;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Algorithm|Share")
	FString MainCanonicalText;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Algorithm|Share")
	bool bHasSetupSequence = false;

	FHyperTwistAlgorithmSequence SetupSequence;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Algorithm|Share")
	FString SetupCanonicalText;

	// Optional validation profile applied during share encode/decode.
	// Empty means "do not enforce a validation profile at this layer".
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Algorithm|Share")
	FString NotationProfile;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Algorithm|Share")
	FString Title;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Algorithm|Share")
	FString PuzzleScope;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Algorithm|Share")
	FString StageScope;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Algorithm|Share")
	FString ViewMode;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Algorithm|Share")
	FString InterpretationMode;
};

UCLASS()
class UNREALHYPERTWIST_API UHyperTwistAlgorithmShareLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Algorithm|Share")
	static bool TrySerializeShareDocumentToJson(
		const FHyperTwistAlgorithmShareDocument& Document,
		FString& OutJson,
		FString& OutError
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Algorithm|Share")
	static bool TryParseShareDocumentFromJson(
		const FString& JsonText,
		FHyperTwistAlgorithmShareDocument& OutDocument,
		FString& OutError
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Algorithm|Share")
	static bool TryEncodeShareDocumentPayload(
		const FHyperTwistAlgorithmShareDocument& Document,
		FString& OutPayload,
		FString& OutError
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Algorithm|Share")
	static bool TryDecodeShareDocumentPayload(
		const FString& Payload,
		FHyperTwistAlgorithmShareDocument& OutDocument,
		FString& OutError
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Algorithm|Share")
	static bool TryBuildShareUrl(
		const FString& BaseUrl,
		const FHyperTwistAlgorithmShareDocument& Document,
		FString& OutUrl,
		FString& OutError
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Algorithm|Share")
	static bool TryParseShareUrl(
		const FString& ShareUrl,
		FHyperTwistAlgorithmShareDocument& OutDocument,
		FString& OutError
	);

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Algorithm|Share")
	static FString GetShareDocumentSchema();

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Algorithm|Share")
	static FString GetDefaultShareQueryKey();
};
