// Copyright HyperTwist, Inc. All Rights Reserved.

#include "Misc/AutomationTest.h"

#include "HyperTwistBootstrap/HyperTwistContractLibrary.h"

#if WITH_AUTOMATION_TESTS

namespace HyperTwistSkillPhaseS5CTestInternal
{
	const FHyperTwistSkillPlanOrchestrationSkill* FindSkillById(
		const FHyperTwistSkillPlanOrchestrationState& PlanOrchestrationState,
		const FString& SkillId
	)
	{
		for (const FHyperTwistSkillPlanOrchestrationSkill& Skill :
			PlanOrchestrationState.Skills)
		{
			if (Skill.SkillId == SkillId)
			{
				return &Skill;
			}
		}

		return nullptr;
	}
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistSkillPhaseS5CPlanOrchestrationCoverageTest,
	"HyperTwist.FirstParty.Skill.PhaseS5C.PlanOrchestrationCoverage",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistSkillPhaseS5CPlanOrchestrationCoverageTest::RunTest(
	const FString& Parameters
)
{
	const FHyperTwistSkillPlanOrchestrationState PlanOrchestrationState =
		UHyperTwistContractLibrary::MakeSampleSkillPlanOrchestrationState();

	TestTrue(
		TEXT("The sample S5-C plan/orchestration state should be structurally valid."),
		PlanOrchestrationState.IsStructurallyValid()
	);
	TestEqual(
		TEXT("S5-C should expose two bounded plan/orchestration skills."),
		PlanOrchestrationState.SkillCount,
		2
	);
	TestEqual(
		TEXT("S5-C should expose one bounded plan-synthesis skill."),
		PlanOrchestrationState.PlanSynthesisSkillCount,
		1
	);
	TestEqual(
		TEXT("S5-C should expose one bounded workflow-chaining skill."),
		PlanOrchestrationState.WorkflowChainingSkillCount,
		1
	);

	const FHyperTwistSkillPlanOrchestrationSkill* PlanSynthesisSkill =
		HyperTwistSkillPhaseS5CTestInternal::FindSkillById(
			PlanOrchestrationState,
			TEXT("skill/synthesize-plan")
		);
	TestNotNull(
		TEXT("The synthesize-plan skill should exist."),
		PlanSynthesisSkill
	);
	if (PlanSynthesisSkill != nullptr)
	{
		TestEqual(
			TEXT("The synthesize-plan skill should bind to the first-party plan-synthesis service."),
			PlanSynthesisSkill->ServiceBindingId,
			FString(TEXT("service/skills/synthesize-plan"))
		);
		TestTrue(
			TEXT("The synthesize-plan skill should declare bounded plan-synthesis mode."),
			PlanSynthesisSkill->bSynthesizesBoundedPlan
		);
	}

	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistSkillPhaseS5CBoundedOrchestrationSafetyTest,
	"HyperTwist.FirstParty.Skill.PhaseS5C.BoundedOrchestrationSafety",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistSkillPhaseS5CBoundedOrchestrationSafetyTest::RunTest(
	const FString& Parameters
)
{
	const FHyperTwistSkillPlanOrchestrationState PlanOrchestrationState =
		UHyperTwistContractLibrary::MakeSampleSkillPlanOrchestrationState();

	TestTrue(
		TEXT("The sample S5-C plan/orchestration state should be structurally valid."),
		PlanOrchestrationState.IsStructurallyValid()
	);
	TestTrue(
		TEXT("Every live S5-C skill should read authoritative workflow, implementation, and memory surfaces."),
		PlanOrchestrationState
			.bEveryLiveSkillReadsAuthoritativeWorkflowImplementationAndMemorySurfaces
	);
	TestTrue(
		TEXT("Every live S5-C skill should keep validation-harness coverage."),
		PlanOrchestrationState.bEveryLiveSkillHasValidationHarnessCoverage
	);
	TestTrue(
		TEXT("S5-C plan/orchestration skills should remain structurally bounded."),
		PlanOrchestrationState.bPlanOrchestrationRemainsBounded
	);
	TestTrue(
		TEXT("S5-C plan/orchestration skills should require the landed implementation/delegation substrate."),
		PlanOrchestrationState.bEverySkillRequiresImplementationDelegationSubstrate
	);
	TestTrue(
		TEXT("S5-C plan/orchestration skills should require the landed workflow-memory substrate."),
		PlanOrchestrationState.bEverySkillRequiresWorkflowMemorySubstrate
	);
	TestTrue(
		TEXT("S5-C plan/orchestration skills should require the landed workflow/review substrate."),
		PlanOrchestrationState.bEverySkillRequiresWorkflowReviewSubstrate
	);
	TestTrue(
		TEXT("S5-C should avoid provider widening."),
		PlanOrchestrationState.bNoProviderWidening
	);
	TestTrue(
		TEXT("S5-C should avoid domain skill-pack widening."),
		PlanOrchestrationState.bNoDomainSkillPackWidening
	);
	TestTrue(
		TEXT("S5-C should avoid autonomous-execution claims."),
		PlanOrchestrationState.bNoAutonomousExecutionClaims
	);

	const FHyperTwistSkillPlanOrchestrationSkill* WorkflowChainSkill =
		HyperTwistSkillPhaseS5CTestInternal::FindSkillById(
			PlanOrchestrationState,
			TEXT("skill/chain-bounded-workflow")
		);
	TestNotNull(
		TEXT("The chain-bounded-workflow skill should exist."),
		WorkflowChainSkill
	);
	if (WorkflowChainSkill != nullptr)
	{
		TestTrue(
			TEXT("The chain-bounded-workflow skill should declare bounded workflow-chaining mode."),
			WorkflowChainSkill->bChainsBoundedWorkflow
		);
		TestEqual(
			TEXT("The chain-bounded-workflow skill should depend on two landed implementation/delegation inputs."),
			WorkflowChainSkill->RequiredImplementationDelegationSkillCount,
			2
		);
		TestEqual(
			TEXT("The chain-bounded-workflow skill should depend on two landed workflow-memory inputs."),
			WorkflowChainSkill->RequiredWorkflowMemorySkillCount,
			2
		);
	}

	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistSkillPhaseS5CSerializationRoundTripTest,
	"HyperTwist.FirstParty.Skill.PhaseS5C.SerializationRoundTrip",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistSkillPhaseS5CSerializationRoundTripTest::RunTest(
	const FString& Parameters
)
{
	const FHyperTwistSkillPlanOrchestrationState PlanOrchestrationState =
		UHyperTwistContractLibrary::MakeSampleSkillPlanOrchestrationState();

	TestTrue(
		TEXT("The sample S5-C plan/orchestration state should be structurally valid before serialization."),
		PlanOrchestrationState.IsStructurallyValid()
	);

	const FString Json =
		UHyperTwistContractLibrary::SerializeSkillPlanOrchestrationStateToJson(
			PlanOrchestrationState
		);
	TestTrue(
		TEXT("Serialized S5-C JSON should include the provider-widening guard field."),
		Json.Contains(TEXT("bNoProviderWidening"))
	);

	FHyperTwistSkillPlanOrchestrationState RoundTrippedState;
	TestTrue(
		TEXT("Deserializing the S5-C plan/orchestration JSON should succeed."),
		UHyperTwistContractLibrary::DeserializeSkillPlanOrchestrationStateFromJson(
			Json,
			RoundTrippedState
		)
	);
	TestTrue(
		TEXT("Round-tripped S5-C plan/orchestration state should remain structurally valid."),
		RoundTrippedState.IsStructurallyValid()
	);
	TestEqual(
		TEXT("Round-tripped S5-C skill count should match the original sample."),
		RoundTrippedState.SkillCount,
		PlanOrchestrationState.SkillCount
	);
	TestEqual(
		TEXT("Round-tripped S5-C workflow-chaining count should match the original sample."),
		RoundTrippedState.WorkflowChainingSkillCount,
		PlanOrchestrationState.WorkflowChainingSkillCount
	);

	return true;
}

#endif
