// Copyright HyperTwist, Inc. All Rights Reserved.

#include "Misc/AutomationTest.h"

#include "HyperTwistBootstrap/HyperTwistContractLibrary.h"

#if WITH_AUTOMATION_TESTS

namespace HyperTwistSkillPhaseS5BTestInternal
{
	const FHyperTwistSkillImplementationDelegationSpec* FindSpecBySkillId(
		const FHyperTwistSkillImplementationDelegationState& ImplementationDelegationState,
		const FString& SkillId
	)
	{
		for (const FHyperTwistSkillImplementationDelegationSpec& Spec :
			ImplementationDelegationState.Specs)
		{
			if (Spec.SkillId == SkillId)
			{
				return &Spec;
			}
		}

		return nullptr;
	}
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistSkillPhaseS5BImplementationDelegationCoverageTest,
	"HyperTwist.FirstParty.Skill.PhaseS5B.ImplementationDelegationCoverage",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistSkillPhaseS5BImplementationDelegationCoverageTest::RunTest(
	const FString& Parameters
)
{
	const FHyperTwistSkillImplementationDelegationState ImplementationDelegationState =
		UHyperTwistContractLibrary::MakeSampleSkillImplementationDelegationState();

	TestTrue(
		TEXT("The sample S5-B implementation/delegation state should be structurally valid."),
		ImplementationDelegationState.IsStructurallyValid()
	);
	TestEqual(
		TEXT("S5-B should expose two bounded implementation/delegation specs."),
		ImplementationDelegationState.SkillCount,
		2
	);
	TestEqual(
		TEXT("S5-B should expose one implementation-review spec."),
		ImplementationDelegationState.ImplementationReviewSpecCount,
		1
	);
	TestEqual(
		TEXT("S5-B should expose one bounded delegation spec."),
		ImplementationDelegationState.DelegationSpecCount,
		1
	);

	const FHyperTwistSkillImplementationDelegationSpec* ImplementationReviewSpec =
		HyperTwistSkillPhaseS5BTestInternal::FindSpecBySkillId(
			ImplementationDelegationState,
			TEXT("skill/review-implementation")
		);
	TestNotNull(
		TEXT("The implementation-review spec should exist."),
		ImplementationReviewSpec
	);
	if (ImplementationReviewSpec != nullptr)
	{
		TestEqual(
			TEXT("The implementation-review spec should bind to the first-party implementation-review service."),
			ImplementationReviewSpec->ServiceBindingId,
			FString(TEXT("service/specs/review-implementation"))
		);
		TestTrue(
			TEXT("The implementation-review spec should declare implementation-review mode."),
			ImplementationReviewSpec->bCoversImplementationReview
		);
	}

	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistSkillPhaseS5BBoundedDelegationSafetyTest,
	"HyperTwist.FirstParty.Skill.PhaseS5B.BoundedDelegationSafety",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistSkillPhaseS5BBoundedDelegationSafetyTest::RunTest(
	const FString& Parameters
)
{
	const FHyperTwistSkillImplementationDelegationState ImplementationDelegationState =
		UHyperTwistContractLibrary::MakeSampleSkillImplementationDelegationState();

	TestTrue(
		TEXT("The sample S5-B implementation/delegation state should be structurally valid."),
		ImplementationDelegationState.IsStructurallyValid()
	);
	TestTrue(
		TEXT("Every S5-B spec should stay in first-party terms."),
		ImplementationDelegationState.bEverySpecUsesFirstPartyTerms
	);
	TestTrue(
		TEXT("Every S5-B spec should preserve the optional-assistive off-state."),
		ImplementationDelegationState.bEverySpecPreservesOptionalAssistiveOffState
	);
	TestTrue(
		TEXT("Every S5-B spec should require provenance-ledger visibility."),
		ImplementationDelegationState.bEverySpecRequiresProvenanceLedger
	);
	TestTrue(
		TEXT("Every S5-B spec should remain bounded to implementation review and delegation only."),
		ImplementationDelegationState.bEverySpecRemainsBounded
	);
	TestTrue(
		TEXT("Every S5-B spec should require the landed workflow/review substrate."),
		ImplementationDelegationState.bEverySpecRequiresWorkflowReviewSubstrate
	);
	TestTrue(
		TEXT("Every S5-B spec should require the landed design-shell substrate."),
		ImplementationDelegationState.bEverySpecRequiresDesignShellSubstrate
	);
	TestTrue(
		TEXT("S5-B should avoid plan-synthesis widening."),
		ImplementationDelegationState.bNoPlanSynthesisWidening
	);
	TestTrue(
		TEXT("S5-B should avoid autonomous-execution claims."),
		ImplementationDelegationState.bNoAutonomousExecutionClaims
	);

	const FHyperTwistSkillImplementationDelegationSpec* DelegationSpec =
		HyperTwistSkillPhaseS5BTestInternal::FindSpecBySkillId(
			ImplementationDelegationState,
			TEXT("skill/specify-bounded-delegation")
		);
	TestNotNull(
		TEXT("The bounded-delegation spec should exist."),
		DelegationSpec
	);
	if (DelegationSpec != nullptr)
	{
		TestTrue(
			TEXT("The bounded-delegation spec should declare bounded delegation mode."),
			DelegationSpec->bDefinesBoundedDelegationSpec
		);
		TestEqual(
			TEXT("The bounded-delegation spec should depend on three landed workflow/review inputs."),
			DelegationSpec->RequiredWorkflowReviewSkillCount,
			3
		);
		TestEqual(
			TEXT("The bounded-delegation spec should depend on two landed design-shell inputs."),
			DelegationSpec->RequiredDesignShellSkillCount,
			2
		);
	}

	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistSkillPhaseS5BSerializationRoundTripTest,
	"HyperTwist.FirstParty.Skill.PhaseS5B.SerializationRoundTrip",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistSkillPhaseS5BSerializationRoundTripTest::RunTest(
	const FString& Parameters
)
{
	const FHyperTwistSkillImplementationDelegationState ImplementationDelegationState =
		UHyperTwistContractLibrary::MakeSampleSkillImplementationDelegationState();

	TestTrue(
		TEXT("The sample S5-B implementation/delegation state should be structurally valid before serialization."),
		ImplementationDelegationState.IsStructurallyValid()
	);

	const FString Json =
		UHyperTwistContractLibrary::SerializeSkillImplementationDelegationStateToJson(
			ImplementationDelegationState
		);
	TestTrue(
		TEXT("Serialized S5-B JSON should include the autonomous-execution guard field."),
		Json.Contains(TEXT("bNoAutonomousExecutionClaims"))
	);

	FHyperTwistSkillImplementationDelegationState RoundTrippedState;
	TestTrue(
		TEXT("Deserializing the S5-B implementation/delegation JSON should succeed."),
		UHyperTwistContractLibrary::DeserializeSkillImplementationDelegationStateFromJson(
			Json,
			RoundTrippedState
		)
	);
	TestTrue(
		TEXT("Round-tripped S5-B implementation/delegation state should remain structurally valid."),
		RoundTrippedState.IsStructurallyValid()
	);
	TestEqual(
		TEXT("Round-tripped S5-B spec count should match the original sample."),
		RoundTrippedState.SpecCount,
		ImplementationDelegationState.SpecCount
	);
	TestEqual(
		TEXT("Round-tripped S5-B safety-rule count should match the original sample."),
		RoundTrippedState.SafetyRuleCount,
		ImplementationDelegationState.SafetyRuleCount
	);

	return true;
}

#endif
