// Copyright HyperTwist, Inc. All Rights Reserved.

#include "Misc/AutomationTest.h"

#include "HyperTwistBootstrap/HyperTwistContractLibrary.h"

#if WITH_AUTOMATION_TESTS

namespace HyperTwistSkillPhaseS7CTestInternal
{
	const FHyperTwistSkillCreativeMediaPackSkill* FindSkillById(
		const FHyperTwistSkillCreativeMediaPackState& PackState,
		const FString& SkillId
	)
	{
		for (const FHyperTwistSkillCreativeMediaPackSkill& Skill :
			PackState.Skills)
		{
			if (Skill.SkillId == SkillId)
			{
				return &Skill;
			}
		}

		return nullptr;
	}
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistSkillPhaseS7CCreativeMediaPackCoverageTest,
	"HyperTwist.FirstParty.Skill.PhaseS7C.CreativeMediaPackCoverage",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistSkillPhaseS7CCreativeMediaPackCoverageTest::RunTest(
	const FString& Parameters
)
{
	const FHyperTwistSkillCreativeMediaPackState PackState =
		UHyperTwistContractLibrary::MakeSampleSkillCreativeMediaPackState();

	TestTrue(
		TEXT("The sample S7-C creative/media-pack state should be structurally valid."),
		PackState.IsStructurallyValid()
	);
	TestEqual(
		TEXT("S7-C should expose three bounded optional creative/media pack skills."),
		PackState.SkillCount,
		3
	);
	TestEqual(
		TEXT("S7-C should expose one replay-explainer pack skill."),
		PackState.ReplayExplainerPackSkillCount,
		1
	);
	TestEqual(
		TEXT("S7-C should expose one media-parser pack skill."),
		PackState.MediaParserPackSkillCount,
		1
	);
	TestEqual(
		TEXT("S7-C should expose one shared-fixture pack skill."),
		PackState.SharedFixturePackSkillCount,
		1
	);

	const FHyperTwistSkillCreativeMediaPackSkill* ReplayExplainerPackSkill =
		HyperTwistSkillPhaseS7CTestInternal::FindSkillById(
			PackState,
			TEXT("skill/compose-replay-explainer-pack")
		);
	TestNotNull(
		TEXT("The compose-replay-explainer-pack skill should exist."),
		ReplayExplainerPackSkill
	);
	if (ReplayExplainerPackSkill != nullptr)
	{
		TestEqual(
			TEXT("The compose-replay-explainer-pack skill should bind to the first-party replay-explainer pack service."),
			ReplayExplainerPackSkill->ServiceBindingId,
			FString(TEXT("service/skills/compose-replay-explainer-pack"))
		);
		TestTrue(
			TEXT("The compose-replay-explainer-pack skill should declare replay-explainer mode."),
			ReplayExplainerPackSkill->bReadsReplayExplainerState
		);
	}

	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistSkillPhaseS7CCreativeMediaPackSafetyTest,
	"HyperTwist.FirstParty.Skill.PhaseS7C.CreativeMediaPackSafety",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistSkillPhaseS7CCreativeMediaPackSafetyTest::RunTest(
	const FString& Parameters
)
{
	const FHyperTwistSkillCreativeMediaPackState PackState =
		UHyperTwistContractLibrary::MakeSampleSkillCreativeMediaPackState();

	TestTrue(
		TEXT("The sample S7-C creative/media-pack state should be structurally valid."),
		PackState.IsStructurallyValid()
	);
	TestTrue(
		TEXT("Every live S7-C skill should read authoritative first-party creative/media state."),
		PackState.bEveryLiveSkillReadsAuthoritativeFirstPartyCreativeMediaState
	);
	TestTrue(
		TEXT("Every live S7-C skill should keep validation-harness coverage."),
		PackState.bEveryLiveSkillHasValidationHarnessCoverage
	);
	TestTrue(
		TEXT("S7-C creative/media packs should remain structurally bounded."),
		PackState.bCreativeMediaPackLayerRemainsBounded
	);
	TestTrue(
		TEXT("S7-C creative/media packs should require the landed retained-domain-pack substrate."),
		PackState.bEverySkillRequiresRetainedDomainPackSubstrate
	);
	TestTrue(
		TEXT("S7-C should keep creative/media adjuncts optional."),
		PackState.bCreativeMediaAdjunctsRemainOptional
	);
	TestTrue(
		TEXT("S7-C should avoid core runtime promotion."),
		PackState.bNoCoreRuntimePromotion
	);
	TestTrue(
		TEXT("S7-C should keep boundary-sensitive licensing explicit."),
		PackState.bBoundarySensitiveLicensingRemainsExplicit
	);

	const FHyperTwistSkillCreativeMediaPackSkill* SharedFixturePackSkill =
		HyperTwistSkillPhaseS7CTestInternal::FindSkillById(
			PackState,
			TEXT("skill/curate-shared-fixture-pack")
		);
	TestNotNull(
		TEXT("The curate-shared-fixture-pack skill should exist."),
		SharedFixturePackSkill
	);
	if (SharedFixturePackSkill != nullptr)
	{
		TestTrue(
			TEXT("The curate-shared-fixture-pack skill should declare shared-fixture mode."),
			SharedFixturePackSkill->bReadsSharedFixtureState
		);
		TestEqual(
			TEXT("The curate-shared-fixture-pack skill should depend on one landed retained-domain-pack input."),
			SharedFixturePackSkill->RequiredRetainedDomainPackSkillCount,
			1
		);
	}

	return true;
}

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistSkillPhaseS7CSerializationRoundTripTest,
	"HyperTwist.FirstParty.Skill.PhaseS7C.SerializationRoundTrip",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistSkillPhaseS7CSerializationRoundTripTest::RunTest(
	const FString& Parameters
)
{
	const FHyperTwistSkillCreativeMediaPackState PackState =
		UHyperTwistContractLibrary::MakeSampleSkillCreativeMediaPackState();

	TestTrue(
		TEXT("The sample S7-C creative/media-pack state should be structurally valid before serialization."),
		PackState.IsStructurallyValid()
	);

	const FString Json =
		UHyperTwistContractLibrary::SerializeSkillCreativeMediaPackStateToJson(
			PackState
		);
	TestTrue(
		TEXT("Serialized S7-C JSON should include the core-runtime guard field."),
		Json.Contains(TEXT("bNoCoreRuntimePromotion"))
	);

	FHyperTwistSkillCreativeMediaPackState RoundTrippedState;
	TestTrue(
		TEXT("Deserializing the S7-C creative/media-pack JSON should succeed."),
		UHyperTwistContractLibrary::DeserializeSkillCreativeMediaPackStateFromJson(
			Json,
			RoundTrippedState
		)
	);
	TestTrue(
		TEXT("Round-tripped S7-C creative/media-pack state should remain structurally valid."),
		RoundTrippedState.IsStructurallyValid()
	);
	TestEqual(
		TEXT("Round-tripped S7-C skill count should match the original sample."),
		RoundTrippedState.SkillCount,
		PackState.SkillCount
	);
	TestEqual(
		TEXT("Round-tripped S7-C shared-fixture pack count should match the original sample."),
		RoundTrippedState.SharedFixturePackSkillCount,
		PackState.SharedFixturePackSkillCount
	);

	return true;
}

#endif
