// Copyright HyperTwist, Inc. All Rights Reserved.

#include "Misc/AutomationTest.h"

#include "HyperTwistBootstrap/HyperTwistContractLibrary.h"

#if WITH_AUTOMATION_TESTS

namespace HyperTwistSkillPhaseS3CTestInternal
{
	const FHyperTwistSkillWorkflowMemoryCaptureSkill* FindSkillById(
		const FHyperTwistSkillWorkflowMemoryCaptureState& WorkflowCaptureState,
		const FString& SkillId
	)
	{
		for (const FHyperTwistSkillWorkflowMemoryCaptureSkill& Skill :
			WorkflowCaptureState.Skills)
		{
			if (Skill.SkillId == SkillId)
			{
				return &Skill;
			}
		}

		return nullptr;
	}
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistSkillPhaseS3CWorkflowMemoryCaptureCoverageTest,
	"HyperTwist.FirstParty.Skill.PhaseS3C.WorkflowMemoryCaptureCoverage",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistSkillPhaseS3CWorkflowMemoryCaptureCoverageTest::RunTest(
	const FString& Parameters
)
{
	const FHyperTwistSkillWorkflowMemoryCaptureState WorkflowCaptureState =
		UHyperTwistContractLibrary::MakeSampleSkillWorkflowMemoryCaptureState();

	TestTrue(
		TEXT("The sample S3-C workflow-memory capture skill state should be structurally valid."),
		WorkflowCaptureState.IsStructurallyValid()
	);
	TestEqual(
		TEXT("S3-C should expose two workflow-memory capture skills."),
		WorkflowCaptureState.SkillCount,
		2
	);
	TestEqual(
		TEXT("S3-C should expose two live workflow-memory capture skills."),
		WorkflowCaptureState.LiveSkillCount,
		2
	);
	TestEqual(
		TEXT("S3-C should expose two currently available workflow-memory capture skills in the sample state."),
		WorkflowCaptureState.AvailableNowCount,
		2
	);

	const FHyperTwistSkillWorkflowMemoryCaptureSkill* TemplateSkill =
		HyperTwistSkillPhaseS3CTestInternal::FindSkillById(
			WorkflowCaptureState,
			TEXT("skill/capture-session-template")
		);
	TestNotNull(
		TEXT("The capture-session-template skill should exist."),
		TemplateSkill
	);
	if (TemplateSkill != nullptr)
	{
		TestEqual(
			TEXT("The capture-session-template skill should bind to the bounded workflow template capture artifact."),
			TemplateSkill->OutputArtifactKinds[0],
			FString(TEXT("artifact/workflow-template-capture-brief"))
		);
	}

	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistSkillPhaseS3CAuthoritativeWorkflowCaptureBoundariesTest,
	"HyperTwist.FirstParty.Skill.PhaseS3C.AuthoritativeWorkflowCaptureBoundaries",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistSkillPhaseS3CAuthoritativeWorkflowCaptureBoundariesTest::RunTest(
	const FString& Parameters
)
{
	const FHyperTwistSkillWorkflowMemoryCaptureState WorkflowCaptureState =
		UHyperTwistContractLibrary::MakeSampleSkillWorkflowMemoryCaptureState();

	TestTrue(
		TEXT("The sample S3-C workflow-memory capture skill state should be structurally valid."),
		WorkflowCaptureState.IsStructurallyValid()
	);
	TestTrue(
		TEXT("Every live S3-C skill should read authoritative stores."),
		WorkflowCaptureState.bEveryLiveSkillReadsAuthoritativeStores
	);
	TestTrue(
		TEXT("Every live S3-C skill should keep validation-harness coverage."),
		WorkflowCaptureState.bEveryLiveSkillHasValidationHarnessCoverage
	);
	TestTrue(
		TEXT("S3-C workflow capture must remain separate from user-authored notes."),
		WorkflowCaptureState.bWorkflowMemoryRemainsSeparateFromUserAuthoredNotes
	);
	TestTrue(
		TEXT("S3-C workflow capture must not collapse into opaque summaries."),
		WorkflowCaptureState.bWorkflowMemoryDoesNotCollapseIntoOpaqueSummaries
	);

	const FHyperTwistSkillWorkflowMemoryCaptureSkill* FollowUpSkill =
		HyperTwistSkillPhaseS3CTestInternal::FindSkillById(
			WorkflowCaptureState,
			TEXT("skill/capture-follow-up-routine")
		);
	TestNotNull(
		TEXT("The capture-follow-up-routine skill should exist."),
		FollowUpSkill
	);
	if (FollowUpSkill != nullptr)
	{
		TestTrue(
			TEXT("The capture-follow-up-routine skill should avoid user-authored note capture."),
			FollowUpSkill->bAvoidsUserAuthoredNoteCapture
		);
		TestTrue(
			TEXT("The capture-follow-up-routine skill should avoid opaque summary collapse."),
			FollowUpSkill->bAvoidsOpaqueSummaryCollapse
		);
		TestEqual(
			TEXT("The capture-follow-up-routine skill should bind to the bounded follow-up capture artifact."),
			FollowUpSkill->OutputArtifactKinds[0],
			FString(TEXT("artifact/follow-up-routine-capture-brief"))
		);
	}

	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistSkillPhaseS3CSerializationRoundTripTest,
	"HyperTwist.FirstParty.Skill.PhaseS3C.SerializationRoundTrip",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistSkillPhaseS3CSerializationRoundTripTest::RunTest(
	const FString& Parameters
)
{
	const FHyperTwistSkillWorkflowMemoryCaptureState WorkflowCaptureState =
		UHyperTwistContractLibrary::MakeSampleSkillWorkflowMemoryCaptureState();

	TestTrue(
		TEXT("The sample S3-C workflow-memory capture skill state should be structurally valid before serialization."),
		WorkflowCaptureState.IsStructurallyValid()
	);

	const FString Json =
		UHyperTwistContractLibrary::SerializeSkillWorkflowMemoryCaptureStateToJson(
			WorkflowCaptureState
		);
	TestTrue(
		TEXT("Serialized S3-C JSON should include the opaque-summary-collapse guard field."),
		Json.Contains(TEXT("bWorkflowMemoryDoesNotCollapseIntoOpaqueSummaries"))
	);

	FHyperTwistSkillWorkflowMemoryCaptureState RoundTrippedState;
	TestTrue(
		TEXT("Deserializing the S3-C workflow-memory capture JSON should succeed."),
		UHyperTwistContractLibrary::DeserializeSkillWorkflowMemoryCaptureStateFromJson(
			Json,
			RoundTrippedState
		)
	);
	TestTrue(
		TEXT("Round-tripped S3-C workflow-memory capture state should remain structurally valid."),
		RoundTrippedState.IsStructurallyValid()
	);
	TestEqual(
		TEXT("Round-tripped S3-C skill count should match the original sample."),
		RoundTrippedState.SkillCount,
		WorkflowCaptureState.SkillCount
	);
	TestEqual(
		TEXT("Round-tripped S3-C live-skill count should match the original sample."),
		RoundTrippedState.LiveSkillCount,
		WorkflowCaptureState.LiveSkillCount
	);

	return true;
}

#endif
