#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"

#include "HyperTwistXrPackageValidationLibrary.generated.h"

class AHyperTwistXrTrainingPawn;

USTRUCT(BlueprintType)
struct UNREALHYPERTWIST_API FHyperTwistXrPackagedValidationReport
{
	GENERATED_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString ReportVersion = TEXT("ht-xr-packaged-validation/v1");

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString GeneratedAtUtc;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString LaunchSurfaceId;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString GameModeClassPath;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString CookMapAssetPath;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString LaunchMapUrl;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString Result = TEXT("failed");

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString PackagedProofStatus;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString RuntimeOwnerId;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString PreferenceProfileId;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString ControllerBindingProfileId;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString ControllerSettingsOwnerId;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString RuntimeStatusLine;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString ReportPath;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	FString Error;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	double ObservationSeconds = 0.0;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	bool bRuntimeOwnerStructurallyValid = false;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	bool bControllerSettingsOwnerStructurallyValid = false;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	bool bHeadMountedDisplayConnected = false;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	bool bHeadMountedDisplayEnabled = false;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	bool bObservedHeadMountedDisplaySession = false;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	bool bObservedControllerInputActivity = false;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	float ObservedControllerInputMagnitude = 0.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HyperTwist|XR|Validation")
	TArray<FString> RemainingGateIds;

	bool IsStructurallyValid() const;
};

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

public:
	UFUNCTION(BlueprintPure, Category = "HyperTwist|XR|Validation")
	static FString GetDefaultPackagedValidationCookMapPath();

	UFUNCTION(BlueprintPure, Category = "HyperTwist|XR|Validation")
	static FString GetValidationLaunchSurfaceId();

	UFUNCTION(BlueprintPure, Category = "HyperTwist|XR|Validation")
	static FString GetValidationGameModeClassPath();

	UFUNCTION(BlueprintPure, Category = "HyperTwist|XR|Validation")
	static FString BuildPackagedValidationMapUrl(const FString& CookMapAssetPath);

	static FHyperTwistXrPackagedValidationReport BuildPackagedValidationReport(
		const AHyperTwistXrTrainingPawn* TrainingPawn,
		const FString& CookMapAssetPath,
		const FString& ReportPath,
		double ObservationSeconds,
		bool bObservedHeadMountedDisplaySession,
		bool bObservedControllerInputActivity,
		float ObservedControllerInputMagnitude
	);

	static bool WritePackagedValidationReportToFile(
		const FHyperTwistXrPackagedValidationReport& Report,
		const FString& ReportPath,
		FString& OutError
	);
};
