#include "HyperTwistBootstrap/HyperTwistContractLibrary.h"

#include "HyperTwistRecognition/HyperTwistRecognitionReplayLibrary.h"
#include "HyperTwistReplay/HyperTwistReplayReviewLibrary.h"
#include "HyperTwistTraining/HyperTwistTrainingCatalogLibrary.h"
#include "HyperTwistTraining/HyperTwistTrainingCoachLibrary.h"
#include "HyperTwistTraining/HyperTwistTrainingLibrary.h"
#include "HyperTwistTraining/HyperTwistTrainingRepositoryLibrary.h"
#include "JsonObjectConverter.h"

namespace HyperTwistContractLibraryInternal
{
	template <typename TStruct>
	FString SerializeStructToJson(const TStruct& Value)
	{
		FString Json;
		FJsonObjectConverter::UStructToJsonObjectString(TStruct::StaticStruct(), &Value, Json, 0, 0);
		return Json;
	}

	template <typename TStruct>
	bool DeserializeStructFromJson(const FString& Json, TStruct& OutValue)
	{
		return FJsonObjectConverter::JsonObjectStringToUStruct(Json, &OutValue, 0, 0);
	}

	FHyperTwistTrainingSourceAttribution MakeMethodDrillCleanRoomAttribution()
	{
		FHyperTwistTrainingSourceAttribution Attribution;
		Attribution.SourceMode = EHyperTwistTrainingSourceMode::CleanRoom;
		Attribution.SourceRepo = TEXT("onionhoney/roux-trainers");
		Attribution.SourceLicense = TEXT("GPLv3 clean-room behavioral handoff");
		Attribution.SourceNotes = TEXT("First-party HyperTwist method-stage drill sample built from scrubbed clean-room guidance.");
		return Attribution;
	}

	FHyperTwistTrainingDeck MakeSampleMethodDrillDeck()
	{
		const FHyperTwistTrainingSourceAttribution Attribution = MakeMethodDrillCleanRoomAttribution();

		FHyperTwistTrainingCase RecognitionCaseA;
		RecognitionCaseA.CaseId = TEXT("roux-block-case-01");
		RecognitionCaseA.PuzzleId = TEXT("cube/3x3x3");
		RecognitionCaseA.PromptKind = EHyperTwistTrainingPromptKind::Recognition;
		RecognitionCaseA.PromptLabel = TEXT("Left Block Recognition A");
		RecognitionCaseA.CanonicalStateRef = TEXT("cube/3x3x3:roux:first-block:left:a");
		RecognitionCaseA.CanonicalNotation = TEXT("R U R'");
		RecognitionCaseA.ScrambleNotation = TEXT("U R U' L' U");
		RecognitionCaseA.SubsetId = TEXT("first-block");
		RecognitionCaseA.Difficulty = 0.45f;
		RecognitionCaseA.Tags = {
			TEXT("classic"),
			TEXT("roux"),
			TEXT("block-building"),
			TEXT("subset:first-block"),
			TEXT("stage-recognition")
		};
		RecognitionCaseA.AllowedDeliveryModes = {
			EHyperTwistTrainingDeliveryMode::VirtualCube,
			EHyperTwistTrainingDeliveryMode::RecognitionAssisted,
			EHyperTwistTrainingDeliveryMode::CoachReviewed
		};
		RecognitionCaseA.TimeTargetMs = 0;
		RecognitionCaseA.SourceAttribution = Attribution;

		FHyperTwistTrainingCase RecognitionCaseB = RecognitionCaseA;
		RecognitionCaseB.CaseId = TEXT("roux-block-case-02");
		RecognitionCaseB.PromptLabel = TEXT("Left Block Recognition B");
		RecognitionCaseB.CanonicalStateRef = TEXT("cube/3x3x3:roux:first-block:left:b");
		RecognitionCaseB.CanonicalNotation = TEXT("r U' r'");
		RecognitionCaseB.ScrambleNotation = TEXT("R U2 R' F U'");
		RecognitionCaseB.Difficulty = 0.62f;

		FHyperTwistTrainingDeck DrillDeck;
		DrillDeck.DeckId = TEXT("roux-first-block-recognition");
		DrillDeck.Title = TEXT("First Block Recognition");
		DrillDeck.DeliveryModes = {
			EHyperTwistTrainingDeliveryMode::VirtualCube,
			EHyperTwistTrainingDeliveryMode::RecognitionAssisted,
			EHyperTwistTrainingDeliveryMode::CoachReviewed
		};
		DrillDeck.SelectionPolicy = EHyperTwistTrainingSelectionPolicy::Scripted;
		DrillDeck.bUsesRealScrambles = false;
		DrillDeck.bSupportsAlgorithmReveal = true;
		DrillDeck.bSupportsVirtualCube = true;
		DrillDeck.bSupportsSmartCube = false;
		DrillDeck.Cases = {RecognitionCaseA, RecognitionCaseB};
		DrillDeck.DifficultyBand = TEXT("stage");
		DrillDeck.Tags = {
			TEXT("classic"),
			TEXT("roux"),
			TEXT("block-building"),
			TEXT("subset:first-block"),
			TEXT("method-drill")
		};
		DrillDeck.SubsetId = TEXT("first-block");
		DrillDeck.SourceAttribution = Attribution;
		return DrillDeck;
	}
}

FHyperTwistPuzzleDefinitionRef UHyperTwistContractLibrary::MakeSampleClassicPuzzleDefinition()
{
	FHyperTwistPuzzleDefinitionRef Definition;
	Definition.PuzzleId = TEXT("cube/3x3x3");
	Definition.PuzzleFamily = EHyperTwistPuzzleFamily::ClassicCube;
	Definition.Dimension = 3;
	Definition.DefinitionVersion = TEXT("2026.04");
	Definition.NotationProfile = TEXT("classic-wca");
	Definition.SizeVector = {3, 3, 3};
	return Definition;
}

FHyperTwistPuzzleDefinitionRef UHyperTwistContractLibrary::MakeSampleHyperPuzzleDefinition()
{
	FHyperTwistPuzzleDefinitionRef Definition;
	Definition.PuzzleId = TEXT("hypercube/2x2x2x2");
	Definition.PuzzleFamily = EHyperTwistPuzzleFamily::Hypercube;
	Definition.Dimension = 4;
	Definition.DefinitionVersion = TEXT("2026.04");
	Definition.NotationProfile = TEXT("hyper-4d");
	Definition.SizeVector = {2, 2, 2, 2};
	return Definition;
}

FHyperTwistPuzzleState UHyperTwistContractLibrary::MakeSampleClassicPuzzleState()
{
	FHyperTwistClassicPieceOrbitState OrbitState;
	OrbitState.Corners.Permutation = {0, 1, 2, 3, 4, 5, 6, 7};
	OrbitState.Corners.Orientation = {0, 0, 0, 0, 0, 0, 0, 0};
	OrbitState.Edges.Permutation = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
	OrbitState.Edges.Orientation = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	OrbitState.Centers.Permutation = {0, 1, 2, 3, 4, 5};
	OrbitState.Centers.Orientation = {0, 0, 0, 0, 0, 0};

	FHyperTwistPuzzleState State;
	State.Definition = MakeSampleClassicPuzzleDefinition();
	State.StateEncodingKind = EHyperTwistStateEncodingKind::PieceOrbit;
	State.StateEncoding.EncodingProfile = OrbitState.EncodingProfile;
	State.StateEncoding.PayloadJson = HyperTwistContractLibraryInternal::SerializeStructToJson(OrbitState);
	State.OrientationFrame.Reference = TEXT("canonical-default");
	State.bIsSolved = true;
	State.Source = EHyperTwistStateSource::Runtime;
	State.CapturedAtUtc = TEXT("2026-04-25T12:00:00Z");
	State.SourceConfidence = -1.0f;
	return State;
}

FHyperTwistPuzzleState UHyperTwistContractLibrary::MakeSampleClassicRecognitionPreviewState()
{
	FHyperTwistClassicFaceletSnapshotState FaceletState;
	FaceletState.U = {TEXT("W"), TEXT("W"), TEXT("W"), TEXT("W"), TEXT("W"), TEXT("W"), TEXT("W"), TEXT("W"), TEXT("W")};
	FaceletState.R = {TEXT("R"), TEXT("R"), TEXT("R"), TEXT("R"), TEXT("R"), TEXT("R"), TEXT("R"), TEXT("R"), TEXT("R")};
	FaceletState.F = {TEXT("G"), TEXT("G"), TEXT("G"), TEXT("G"), TEXT("G"), TEXT("G"), TEXT("G"), TEXT("G"), TEXT("G")};
	FaceletState.D = {TEXT("Y"), TEXT("Y"), TEXT("Y"), TEXT("Y"), TEXT("Y"), TEXT("Y"), TEXT("Y"), TEXT("Y"), TEXT("Y")};
	FaceletState.L = {TEXT("O"), TEXT("O"), TEXT("O"), TEXT("O"), TEXT("O"), TEXT("O"), TEXT("O"), TEXT("O"), TEXT("O")};
	FaceletState.B = {TEXT("B"), TEXT("B"), TEXT("B"), TEXT("B"), TEXT("B"), TEXT("B"), TEXT("B"), TEXT("B"), TEXT("B")};

	FHyperTwistPuzzleState State;
	State.Definition = MakeSampleClassicPuzzleDefinition();
	State.StateEncodingKind = EHyperTwistStateEncodingKind::Facelet;
	State.StateEncoding.EncodingProfile = FaceletState.EncodingProfile;
	State.StateEncoding.PayloadJson = HyperTwistContractLibraryInternal::SerializeStructToJson(FaceletState);
	State.OrientationFrame.Reference = TEXT("camera-relative");
	State.bIsSolved = false;
	State.Source = EHyperTwistStateSource::Recognition;
	State.CapturedAtUtc = TEXT("2026-04-25T12:00:00Z");
	State.SourceConfidence = 0.84f;
	State.SourceSessionId = TEXT("vision_01");
	return State;
}

FHyperTwistPuzzleState UHyperTwistContractLibrary::MakeSampleHyperPuzzleState()
{
	FHyperTwistHypercubePieceMapState HyperState;
	HyperState.Corners = {TEXT("c0"), TEXT("c1"), TEXT("c2"), TEXT("c3")};
	HyperState.Edges = {TEXT("e0"), TEXT("e1"), TEXT("e2"), TEXT("e3")};
	HyperState.Faces = {TEXT("f0"), TEXT("f1"), TEXT("f2"), TEXT("f3")};
	HyperState.Cells = {TEXT("cell0"), TEXT("cell1"), TEXT("cell2"), TEXT("cell3")};

	FHyperTwistPuzzleState State;
	State.Definition = MakeSampleHyperPuzzleDefinition();
	State.StateEncodingKind = EHyperTwistStateEncodingKind::FamilySpecific;
	State.StateEncoding.EncodingProfile = HyperState.EncodingProfile;
	State.StateEncoding.PayloadJson = HyperTwistContractLibraryInternal::SerializeStructToJson(HyperState);
	State.OrientationFrame.Reference = TEXT("hypercube-canonical-v1");
	State.bIsSolved = true;
	State.Source = EHyperTwistStateSource::Runtime;
	State.CapturedAtUtc = TEXT("2026-04-25T12:00:00Z");
	return State;
}

FHyperTwistTransformation UHyperTwistContractLibrary::MakeSampleClassicTransformation()
{
	FHyperTwistClassicTransformEncoding TransformEncoding;
	TransformEncoding.MoveSequence = {TEXT("R"), TEXT("U"), TEXT("R'"), TEXT("U'")};

	FHyperTwistTransformation Transformation;
	Transformation.Definition = MakeSampleClassicPuzzleDefinition();
	Transformation.TransformKind = EHyperTwistTransformKind::Algorithm;
	Transformation.Notation = TEXT("R U R' U'");
	Transformation.TransformEncoding.EncodingProfile = TransformEncoding.EncodingProfile;
	Transformation.TransformEncoding.PayloadJson = HyperTwistContractLibraryInternal::SerializeStructToJson(TransformEncoding);
	Transformation.OriginalNotation = Transformation.Notation;
	return Transformation;
}

FHyperTwistReplayPacket UHyperTwistContractLibrary::MakeSampleRuntimeReplayPacket()
{
	FHyperTwistReplayPhaseMarkPayload PhasePayload;
	PhasePayload.PhaseId = TEXT("phase_exec");
	PhasePayload.PhaseKind = TEXT("execution");
	PhasePayload.PhaseSource = TEXT("explicit");
	PhasePayload.PhaseAction = TEXT("start");

	FHyperTwistReplayMovePayload MovePayload;
	MovePayload.Notation = TEXT("R");
	MovePayload.Source = TEXT("runtime");

	FHyperTwistReplayEvent TimerStartEvent;
	TimerStartEvent.EventId = TEXT("evt_01");
	TimerStartEvent.Sequence = 1;
	TimerStartEvent.TimeMs = 0;
	TimerStartEvent.EventType = EHyperTwistReplayEventType::TimerStart;

	FHyperTwistReplayEvent PhaseEvent;
	PhaseEvent.EventId = TEXT("evt_02");
	PhaseEvent.Sequence = 2;
	PhaseEvent.TimeMs = 100;
	PhaseEvent.EventType = EHyperTwistReplayEventType::PhaseMark;
	PhaseEvent.PayloadJson = HyperTwistContractLibraryInternal::SerializeStructToJson(PhasePayload);

	FHyperTwistReplayEvent MoveEvent;
	MoveEvent.EventId = TEXT("evt_03");
	MoveEvent.Sequence = 3;
	MoveEvent.TimeMs = 250;
	MoveEvent.EventType = EHyperTwistReplayEventType::Move;
	MoveEvent.PayloadJson = HyperTwistContractLibraryInternal::SerializeStructToJson(MovePayload);

	FHyperTwistPhaseTiming ExecutionTiming;
	ExecutionTiming.PhaseKind = TEXT("execution");
	ExecutionTiming.StartedAtMs = 100;
	ExecutionTiming.EndedAtMs = 12450;
	ExecutionTiming.DurationMs = 12350;

	FHyperTwistReplayPacket Packet;
	Packet.ReplayId = TEXT("replay_runtime_01");
	Packet.PuzzleDefinition = MakeSampleClassicPuzzleDefinition();
	Packet.CaptureMode = EHyperTwistCaptureMode::Runtime;
	Packet.SessionId = TEXT("session_runtime_01");
	Packet.StartedAtUtc = TEXT("2026-04-25T12:00:00Z");
	Packet.EndedAtUtc = TEXT("2026-04-25T12:00:12Z");
	Packet.Events = {TimerStartEvent, PhaseEvent, MoveEvent};
	Packet.DerivedSummary.TotalTimeMs = 12450;
	Packet.DerivedSummary.MoveCount = 56;
	Packet.DerivedSummary.TPS = 4.93f;
	Packet.DerivedSummary.PhaseTimings = {ExecutionTiming};
	Packet.DerivedSummary.Result = TEXT("completed");
	return Packet;
}

FHyperTwistReplayPacket UHyperTwistContractLibrary::MakeSampleRecognitionReplayPacket()
{
	FHyperTwistReplayRecognitionPayload PreviewPayload;
	PreviewPayload.Snapshot.SnapshotId = TEXT("snap_preview");
	PreviewPayload.Snapshot.State = MakeSampleClassicRecognitionPreviewState();
	PreviewPayload.Snapshot.DerivedHash = TEXT("preview_hash");
	PreviewPayload.Confidence = 0.78f;
	PreviewPayload.ScanStage = TEXT("preview");

	FHyperTwistReplayRecognitionPayload CommitPayload;
	CommitPayload.Snapshot.SnapshotId = TEXT("snap_commit");
	CommitPayload.Snapshot.State = MakeSampleClassicPuzzleState();
	CommitPayload.Snapshot.State.Source = EHyperTwistStateSource::Recognition;
	CommitPayload.Snapshot.State.SourceConfidence = 0.96f;
	CommitPayload.Snapshot.State.SourceSessionId = TEXT("vision_01");
	CommitPayload.Snapshot.DerivedHash = TEXT("commit_hash");
	CommitPayload.Confidence = 0.96f;
	CommitPayload.CommittedFaceOrStage = TEXT("F");
	CommitPayload.ScanStage = TEXT("commit");

	FHyperTwistReplayEvent PreviewEvent;
	PreviewEvent.EventId = TEXT("evt_01");
	PreviewEvent.Sequence = 1;
	PreviewEvent.TimeMs = 0;
	PreviewEvent.EventType = EHyperTwistReplayEventType::RecognitionPreview;
	PreviewEvent.PayloadJson = HyperTwistContractLibraryInternal::SerializeStructToJson(PreviewPayload);

	FHyperTwistReplayEvent CommitEvent;
	CommitEvent.EventId = TEXT("evt_02");
	CommitEvent.Sequence = 2;
	CommitEvent.TimeMs = 420;
	CommitEvent.EventType = EHyperTwistReplayEventType::RecognitionCommit;
	CommitEvent.PayloadJson = HyperTwistContractLibraryInternal::SerializeStructToJson(CommitPayload);

	FHyperTwistReplayPacket Packet;
	Packet.ReplayId = TEXT("replay_vision_01");
	Packet.PuzzleDefinition = MakeSampleClassicPuzzleDefinition();
	Packet.CaptureMode = EHyperTwistCaptureMode::Recognition;
	Packet.SessionId = TEXT("vision_01");
	Packet.StartedAtUtc = TEXT("2026-04-25T12:00:00Z");
	Packet.EndedAtUtc = TEXT("2026-04-25T12:00:01Z");
	Packet.Events = {PreviewEvent, CommitEvent};
	Packet.DerivedSummary.TotalTimeMs = 420;
	Packet.DerivedSummary.RecognizedSegments = 1;
	Packet.DerivedSummary.ConfidenceRollup = 0.96f;
	Packet.DerivedSummary.Result = TEXT("committed");
	return Packet;
}

FHyperTwistVisionSessionConfig UHyperTwistContractLibrary::MakeSampleVisionSessionConfig()
{
	FHyperTwistVisionSessionConfig Config;
	Config.SessionId = TEXT("vision_01");
	Config.PuzzleId = TEXT("cube/3x3x3");
	Config.ExpectedPuzzleFamily = EHyperTwistPuzzleFamily::ClassicCube;
	Config.NotationProfile = TEXT("classic-wca");
	Config.FrameSize = FIntPoint(1280, 720);
	Config.MaxFrameRate = 30;
	return Config;
}

FHyperTwistVisionPreviewResult UHyperTwistContractLibrary::MakeMockVisionPreviewResult()
{
	FHyperTwistVisionPreviewResult Result;
	Result.SessionId = TEXT("vision_01");
	Result.FrameId = TEXT("frame_10");
	Result.PreviewConfidence = 0.84f;
	Result.PreviewState.SnapshotId = TEXT("snap_prev_10");
	Result.PreviewState.State = MakeSampleClassicRecognitionPreviewState();
	Result.PreviewState.DerivedHash = TEXT("preview_hash");
	Result.Guidance = TEXT("capture-ready");
	return Result;
}

FHyperTwistVisionCommitResult UHyperTwistContractLibrary::MakeMockVisionCommitResult()
{
	FHyperTwistVisionCommitResult Result;
	Result.SessionId = TEXT("vision_01");
	Result.CommitKind = TEXT("face");
	Result.Snapshot.SnapshotId = TEXT("snap_commit");
	Result.Snapshot.State = MakeSampleClassicPuzzleState();
	Result.Snapshot.State.Source = EHyperTwistStateSource::Recognition;
	Result.Snapshot.State.SourceConfidence = 0.96f;
	Result.Snapshot.State.SourceSessionId = TEXT("vision_01");
	Result.Snapshot.DerivedHash = TEXT("commit_hash");
	Result.Confidence = 0.96f;
	Result.CommittedUnit = TEXT("F");
	return Result;
}

FHyperTwistVisionFinalizeResult UHyperTwistContractLibrary::MakeMockVisionFinalizeResult()
{
	FHyperTwistVisionFinalizeResult Result;
	Result.SessionId = TEXT("vision_01");
	Result.FinalState = MakeSampleClassicPuzzleState();
	Result.FinalState.Source = EHyperTwistStateSource::Recognition;
	Result.FinalState.SourceConfidence = 0.97f;
	Result.FinalState.SourceSessionId = TEXT("vision_01");
	Result.FinalSnapshot.SnapshotId = TEXT("snap_final");
	Result.FinalSnapshot.State = Result.FinalState;
	Result.FinalSnapshot.DerivedHash = TEXT("final_hash");
	Result.ReplaySeedEventsJson.Add(SerializeReplayPacketToJson(MakeSampleRecognitionReplayPacket()));
	Result.ConfidenceRollup = 0.97f;
	return Result;
}

FHyperTwistVisionServiceHealth UHyperTwistContractLibrary::MakeMockVisionServiceHealth()
{
	FHyperTwistVisionServiceHealth Health;
	Health.ProviderLabel = TEXT("mock");
	Health.ServiceVersion = TEXT("mock-vision/v1");
	Health.ServiceEndpoint = TEXT("in-process://mock");
	Health.Capabilities = {TEXT("liveCamera3x3"), TEXT("faceCommitFlow"), TEXT("calibrationProfiles")};
	Health.bReady = true;
	return Health;
}

FHyperTwistContentPack UHyperTwistContractLibrary::MakeSampleAlgTrainerContentPack()
{
	return UHyperTwistTrainingCatalogLibrary::MakeAlgTrainerStarterContentPack();
}

FHyperTwistContentPack UHyperTwistContractLibrary::MakeSampleCrossTrainerContentPack()
{
	return UHyperTwistTrainingCatalogLibrary::MakeCrossTrainerStarterContentPack();
}

FHyperTwistContentPack UHyperTwistContractLibrary::MakeSampleFiveStyleContentPack()
{
	return UHyperTwistTrainingCatalogLibrary::MakeFiveStyleStarterContentPack();
}

FHyperTwistTrainingCatalogImportLoadReport UHyperTwistContractLibrary::MakeBundledCatalogMaterializationLoadReport()
{
	FHyperTwistTrainingCatalogMaterializedBundle MaterializedBundle;
	FHyperTwistTrainingCatalogImportLoadReport LoadReport;
	UHyperTwistTrainingCatalogLibrary::LoadBundledCatalogMaterializationBundle(
		MaterializedBundle,
		LoadReport
	);
	return LoadReport;
}

FHyperTwistTrainingRunState UHyperTwistContractLibrary::MakeSampleTrainingRunState()
{
	const FHyperTwistContentPack ContentPack = MakeSampleAlgTrainerContentPack();
	FHyperTwistTrainingDeck DefaultDeck;
	if (!UHyperTwistTrainingCatalogLibrary::TryGetDefaultDeckFromContentPack(ContentPack, DefaultDeck))
	{
		return FHyperTwistTrainingRunState();
	}

	return UHyperTwistTrainingLibrary::StartTrainingRun(
		DefaultDeck,
		TEXT("local-user"),
		TEXT("training_session_01"),
		EHyperTwistTrainingDeliveryMode::Timer
	);
}

FHyperTwistTrainingMethodDrillDefinition UHyperTwistContractLibrary::MakeSampleMethodDrillDefinition()
{
	FHyperTwistTrainingMethodDrillDefinition DrillDefinition =
		UHyperTwistTrainingLibrary::BuildMethodDrillDefinitionFromDeck(
			HyperTwistContractLibraryInternal::MakeSampleMethodDrillDeck(),
			EHyperTwistTrainingDrillModeKind::Recognition,
			EHyperTwistTrainingDrillVisibilityPolicy::MaskedState,
			true,
			EHyperTwistTrainingDrillEntrySource::Catalog
		);
	DrillDefinition.Title = TEXT("Roux First Block Recognition Drill");
	DrillDefinition.StageId = TEXT("first-block");
	DrillDefinition.StageLabel = TEXT("First Block");
	return DrillDefinition;
}

FHyperTwistTrainingMethodDrillRunState UHyperTwistContractLibrary::MakeSampleMethodDrillRunState()
{
	const FHyperTwistTrainingMethodDrillDefinition DrillDefinition = MakeSampleMethodDrillDefinition();
	if (!DrillDefinition.IsStructurallyValid())
	{
		return FHyperTwistTrainingMethodDrillRunState();
	}

	const FHyperTwistTrainingMethodDrillRunState ReadyRun = UHyperTwistTrainingLibrary::StartMethodDrillRun(
		DrillDefinition,
		TEXT("local-user"),
		TEXT("method_drill_run_01")
	);

	FHyperTwistTrainingMethodDrillRunState ActiveRun = UHyperTwistTrainingLibrary::StartNextMethodDrillCase(ReadyRun);
	if (!ActiveRun.IsStructurallyValid())
	{
		return FHyperTwistTrainingMethodDrillRunState();
	}

	ActiveRun.StartedAtUtc = TEXT("2026-04-25T12:12:00Z");
	ActiveRun.LastUpdatedAtUtc = TEXT("2026-04-25T12:12:05Z");
	return ActiveRun;
}

FHyperTwistTrainingMethodDrillSummary UHyperTwistContractLibrary::MakeSampleMethodDrillSummary()
{
	return UHyperTwistTrainingLibrary::DeriveMethodDrillSummary(MakeSampleMethodDrillRunState());
}

FHyperTwistTrainingMethodDrillFavoriteEntry UHyperTwistContractLibrary::MakeSampleMethodDrillFavoriteEntry()
{
	return UHyperTwistTrainingRepositoryLibrary::MakeMethodDrillFavoriteEntry(
		MakeSampleMethodDrillDefinition(),
		TEXT("local-user"),
		TEXT("roux-block-case-01"),
		TEXT("2026-04-25T12:11:00Z")
	);
}

FHyperTwistTrainingMethodDrillBatch UHyperTwistContractLibrary::MakeSampleMethodDrillBatch()
{
	FHyperTwistTrainingMethodDrillRunState RunState = MakeSampleMethodDrillRunState();
	if (!RunState.IsStructurallyValid())
	{
		return FHyperTwistTrainingMethodDrillBatch();
	}

	RunState = UHyperTwistTrainingLibrary::RevealMethodDrillCase(RunState);

	FHyperTwistTrainingMethodDrillResponseRecord ResponseRecord;
	ResponseRecord.ResponseId = TEXT("method_drill_response_01");
	ResponseRecord.DrillRunId = RunState.RunId;
	ResponseRecord.CaseId = RunState.CurrentCase.CaseId;
	ResponseRecord.PresentedAtUtc = RunState.StartedAtUtc;
	ResponseRecord.RespondedAtUtc = TEXT("2026-04-25T12:13:00Z");
	ResponseRecord.DrillMode = RunState.DrillDefinition.DrillMode;
	ResponseRecord.VisibilityPolicy = RunState.DrillDefinition.VisibilityPolicy;
	ResponseRecord.bAnswerWasRevealed = true;
	ResponseRecord.bMarkedCorrect = true;
	ResponseRecord.bWasRepeat = false;
	ResponseRecord.ElapsedMs = 1850;
	ResponseRecord.ResponseText = TEXT("identified left block anchor and follow-up pair");
	RunState.Responses.Add(ResponseRecord);
	RunState.LastUpdatedAtUtc = ResponseRecord.RespondedAtUtc;

	return UHyperTwistTrainingLibrary::BuildMethodDrillBatch(
		RunState,
		TEXT("method_drill_batch_01"),
		TEXT("Pinned Roux Recognition Queue"),
		true
	);
}

FHyperTwistTrainingMethodDrillDefinition UHyperTwistContractLibrary::MakeSampleFavoriteFirstMethodDrillDefinition()
{
	const FHyperTwistTrainingMethodDrillDefinition BaseDefinition = MakeSampleMethodDrillDefinition();
	if (!BaseDefinition.IsStructurallyValid())
	{
		return FHyperTwistTrainingMethodDrillDefinition();
	}

	TArray<FHyperTwistTrainingMethodDrillFavoriteEntry> Favorites;
	FHyperTwistTrainingMethodDrillFavoriteEntry FavoriteEntry = MakeSampleMethodDrillFavoriteEntry();
	FavoriteEntry.CaseId = TEXT("roux-block-case-02");
	Favorites.Add(FavoriteEntry);
	return UHyperTwistTrainingRepositoryLibrary::BuildDrillDefinitionFavoriteFirst(
		BaseDefinition,
		Favorites,
		1
	);
}

FHyperTwistTrainingMethodDrillBatchReadiness UHyperTwistContractLibrary::MakeSampleMethodDrillBatchReadiness()
{
	return UHyperTwistTrainingRepositoryLibrary::DeriveDrillBatchReadiness(
		MakeSampleMethodDrillBatch(),
		MakeSampleMethodDrillDefinition()
	);
}

FHyperTwistTrainingDrillCaseModeCompatibility UHyperTwistContractLibrary::MakeSampleMethodDrillCaseModeCompatibility()
{
	const FHyperTwistTrainingMethodDrillDefinition DrillDefinition = MakeSampleMethodDrillDefinition();
	if (!DrillDefinition.IsStructurallyValid() || DrillDefinition.SourceDeck.Cases.Num() == 0)
	{
		return FHyperTwistTrainingDrillCaseModeCompatibility();
	}

	return UHyperTwistTrainingLibrary::DeriveDrillCaseModeCompatibility(
		DrillDefinition.SourceDeck.Cases[0],
		DrillDefinition.DrillMode,
		DrillDefinition.VisibilityPolicy
	);
}

FHyperTwistTrainingMethodDrillDiagnosticPacket UHyperTwistContractLibrary::MakeSampleMethodDrillDiagnosticPacket()
{
	const FHyperTwistTrainingMethodDrillDefinition DrillDefinition = MakeSampleMethodDrillDefinition();
	if (!DrillDefinition.IsStructurallyValid())
	{
		return FHyperTwistTrainingMethodDrillDiagnosticPacket();
	}

	FHyperTwistTrainingMethodDrillRunState RunState = UHyperTwistTrainingLibrary::StartMethodDrillRun(
		DrillDefinition,
		TEXT("local-user"),
		TEXT("method_drill_run_diagnostics_01")
	);
	if (!RunState.IsStructurallyValid())
	{
		return FHyperTwistTrainingMethodDrillDiagnosticPacket();
	}

	RunState.StartedAtUtc = TEXT("2026-04-29T09:00:00Z");
	RunState.LastUpdatedAtUtc = RunState.StartedAtUtc;
	RunState = UHyperTwistTrainingLibrary::StartNextMethodDrillCase(RunState);
	if (!RunState.IsStructurallyValid())
	{
		return FHyperTwistTrainingMethodDrillDiagnosticPacket();
	}

	RunState = UHyperTwistTrainingLibrary::RevealMethodDrillCase(RunState);

	FHyperTwistTrainingMethodDrillResponseRecord AssistedResponse;
	AssistedResponse.ResponseId = TEXT("method_drill_diag_response_01");
	AssistedResponse.DrillRunId = RunState.RunId;
	AssistedResponse.CaseId = RunState.CurrentCase.CaseId;
	AssistedResponse.PresentedAtUtc = TEXT("2026-04-29T09:00:05Z");
	AssistedResponse.RespondedAtUtc = TEXT("2026-04-29T09:00:17Z");
	AssistedResponse.DrillMode = RunState.DrillDefinition.DrillMode;
	AssistedResponse.VisibilityPolicy = RunState.DrillDefinition.VisibilityPolicy;
	AssistedResponse.bAnswerWasRevealed = true;
	AssistedResponse.bMarkedCorrect = true;
	AssistedResponse.bWasRepeat = false;
	AssistedResponse.ElapsedMs = 1700;
	AssistedResponse.ResponseText = TEXT("recognized left block after reveal");
	RunState.Responses.Add(AssistedResponse);
	RunState.LastUpdatedAtUtc = AssistedResponse.RespondedAtUtc;
	RunState = UHyperTwistTrainingLibrary::AdvanceMethodDrillCase(RunState);
	RunState = UHyperTwistTrainingLibrary::StartNextMethodDrillCase(RunState);
	if (!RunState.IsStructurallyValid())
	{
		return FHyperTwistTrainingMethodDrillDiagnosticPacket();
	}

	FHyperTwistTrainingMethodDrillResponseRecord RepeatResponseA;
	RepeatResponseA.ResponseId = TEXT("method_drill_diag_response_02");
	RepeatResponseA.DrillRunId = RunState.RunId;
	RepeatResponseA.CaseId = RunState.CurrentCase.CaseId;
	RepeatResponseA.PresentedAtUtc = TEXT("2026-04-29T09:00:20Z");
	RepeatResponseA.RespondedAtUtc = TEXT("2026-04-29T09:00:31Z");
	RepeatResponseA.DrillMode = RunState.DrillDefinition.DrillMode;
	RepeatResponseA.VisibilityPolicy = RunState.DrillDefinition.VisibilityPolicy;
	RepeatResponseA.bAnswerWasRevealed = false;
	RepeatResponseA.bMarkedCorrect = false;
	RepeatResponseA.bWasRepeat = false;
	RepeatResponseA.ElapsedMs = 2400;
	RepeatResponseA.ResponseText = TEXT("missed block anchor on first pass");
	RunState.Responses.Add(RepeatResponseA);
	RunState.LastUpdatedAtUtc = RepeatResponseA.RespondedAtUtc;
	RunState = UHyperTwistTrainingLibrary::RepeatMethodDrillCase(RunState);

	FHyperTwistTrainingMethodDrillResponseRecord RepeatResponseB;
	RepeatResponseB.ResponseId = TEXT("method_drill_diag_response_03");
	RepeatResponseB.DrillRunId = RunState.RunId;
	RepeatResponseB.CaseId = RunState.CurrentCase.CaseId;
	RepeatResponseB.PresentedAtUtc = TEXT("2026-04-29T09:00:35Z");
	RepeatResponseB.RespondedAtUtc = TEXT("2026-04-29T09:00:42Z");
	RepeatResponseB.DrillMode = RunState.DrillDefinition.DrillMode;
	RepeatResponseB.VisibilityPolicy = RunState.DrillDefinition.VisibilityPolicy;
	RepeatResponseB.bAnswerWasRevealed = false;
	RepeatResponseB.bMarkedCorrect = true;
	RepeatResponseB.bWasRepeat = true;
	RepeatResponseB.ElapsedMs = 1900;
	RepeatResponseB.ResponseText = TEXT("corrected block anchor after repeat");
	RunState.Responses.Add(RepeatResponseB);
	RunState.LastUpdatedAtUtc = RepeatResponseB.RespondedAtUtc;

	return UHyperTwistTrainingLibrary::DeriveMethodDrillDiagnosticPacket(RunState);
}

FHyperTwistTrainingSessionTemplate UHyperTwistContractLibrary::MakeSampleMethodDrillFollowUpTemplate()
{
	return UHyperTwistTrainingRepositoryLibrary::BuildMethodDrillFollowUpTemplate(
		MakeSampleMethodDrillDiagnosticPacket(),
		TEXT("method_drill_follow_up_template_01")
	);
}

FHyperTwistTrainingRunStepResult UHyperTwistContractLibrary::MakeSampleTrainingRunStepResult()
{
	const FHyperTwistTrainingRunState RunState = MakeSampleTrainingRunState();
	if (!RunState.IsStructurallyValid())
	{
		return FHyperTwistTrainingRunStepResult();
	}

	FHyperTwistTrainingAttempt Attempt;
	Attempt.AttemptId = TEXT("attempt_01");
	Attempt.TrainingSessionId = RunState.Session.TrainingSessionId;
	Attempt.CaseId = RunState.Session.CurrentCaseId;
	Attempt.Result = EHyperTwistTrainingAttemptResult::Success;
	Attempt.RecognitionTimeMs = 0;
	Attempt.Mistakes = 0;
	Attempt.ReplayId = RunState.ReplayPacket.ReplayId;
	Attempt.CompletedAtUtc = TEXT("2026-04-25T12:02:14Z");
	Attempt.TimingBreakdown = MakeSampleTrainingTimingBreakdown();

	return UHyperTwistTrainingLibrary::ResolveTrainingRunAttempt(RunState, Attempt, 0);
}

FHyperTwistTrainingSessionSummary UHyperTwistContractLibrary::MakeSampleTrainingRunSummary()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	return StepResult.SessionSummary;
}

FHyperTwistTrainingTimingPolicy UHyperTwistContractLibrary::MakeSampleTrainingTimingPolicy()
{
	FHyperTwistTrainingTimingPolicy TimingPolicy;
	TimingPolicy.bInspectionEnabled = true;
	TimingPolicy.InspectionDurationMs = 15000;
	TimingPolicy.bInspectionPenaltiesEnabled = true;
	TimingPolicy.bAllowManualPenalty = true;
	TimingPolicy.bHideRunningTime = false;

	FHyperTwistTrainingSplitPhaseDefinition RecognitionPhase;
	RecognitionPhase.PhaseId = TEXT("recognition");
	RecognitionPhase.Label = TEXT("Recognition");
	RecognitionPhase.Order = 0;

	FHyperTwistTrainingSplitPhaseDefinition ExecutionPhase;
	ExecutionPhase.PhaseId = TEXT("execution");
	ExecutionPhase.Label = TEXT("Execution");
	ExecutionPhase.Order = 1;

	TimingPolicy.SplitPhases = {RecognitionPhase, ExecutionPhase};
	return TimingPolicy;
}

FHyperTwistTrainingTimingBreakdown UHyperTwistContractLibrary::MakeSampleTrainingTimingBreakdown()
{
	FHyperTwistTrainingTimingBreakdown TimingBreakdown;
	TimingBreakdown.InspectionElapsedMs = 14750;
	TimingBreakdown.RawSolveTimeMs = 2214;
	TimingBreakdown.ManualPenalty = EHyperTwistTrainingPenalty::None;
	TimingBreakdown.InspectionPenalty = EHyperTwistTrainingPenalty::None;
	TimingBreakdown.AppliedPenalty = EHyperTwistTrainingPenalty::None;
	TimingBreakdown.PenaltyOffsetMs = 0;
	TimingBreakdown.FinalTimeMs = 2214;
	TimingBreakdown.bDidNotFinish = false;

	FHyperTwistTrainingSplitCapture RecognitionCapture;
	RecognitionCapture.PhaseId = TEXT("recognition");
	RecognitionCapture.Label = TEXT("Recognition");
	RecognitionCapture.TimestampMs = 620;

	TimingBreakdown.SplitCaptures = {RecognitionCapture};
	return TimingBreakdown;
}

FHyperTwistTrainingRepositoryState UHyperTwistContractLibrary::MakeSampleTrainingRepositoryState()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingRepositoryState();
	}

	FHyperTwistTrainingRepositoryState RepositoryState;
	RepositoryState = UHyperTwistTrainingRepositoryLibrary::RecordRunState(RepositoryState, StepResult.UpdatedRunState);

	const TArray<FHyperTwistTrainingSessionTemplate> DerivedTemplates =
		UHyperTwistTrainingRepositoryLibrary::DeriveTrainingSessionTemplates(
			RepositoryState,
			StepResult.UpdatedRunState.Session.UserId,
			TEXT("2026-04-28T12:05:00Z")
		);
	if (DerivedTemplates.Num() > 0)
	{
		FHyperTwistTrainingSessionTemplate CustomizedTemplate = DerivedTemplates[0];
		CustomizedTemplate.Title = TEXT("Pinned Recovery Review");
		CustomizedTemplate.GuidanceLabel = TEXT("user-customized-recovery");
		CustomizedTemplate.RecommendedCaseCount = FMath::Max(2, CustomizedTemplate.RecommendedCaseCount);
		CustomizedTemplate.SuggestedMode = EHyperTwistTrainingDeliveryMode::VirtualCube;
		CustomizedTemplate.SourceTemplateId = CustomizedTemplate.TemplateId;
		CustomizedTemplate.bUserCustomized = true;
		CustomizedTemplate.LastUpdatedAtUtc = TEXT("2026-04-28T12:10:00Z");
		RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertStoredTrainingSessionTemplate(
			RepositoryState,
			CustomizedTemplate
		);
	}

	const FHyperTwistTrainingMethodDrillFavoriteEntry FavoriteEntry = MakeSampleMethodDrillFavoriteEntry();
	if (FavoriteEntry.IsStructurallyValid())
	{
		RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertMethodDrillFavorite(
			RepositoryState,
			FavoriteEntry
		);
	}

	const FHyperTwistTrainingMethodDrillBatch DrillBatch = MakeSampleMethodDrillBatch();
	if (DrillBatch.IsStructurallyValid())
	{
		RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertMethodDrillBatch(
			RepositoryState,
			DrillBatch
		);
	}

	const FHyperTwistTrainingCoachActionPlan CoachActionPlan = MakeSampleTrainingCoachActionPlan();
	if (CoachActionPlan.IsStructurallyValid())
	{
		RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachActionPlan(
			RepositoryState,
			CoachActionPlan
		);
	}

	const FHyperTwistTrainingCoachSessionQueueState CoachSessionQueueState =
		MakeSampleTrainingCoachSessionQueueState();
	if (CoachSessionQueueState.IsStructurallyValid())
	{
		RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachSessionQueueState(
			RepositoryState,
			CoachSessionQueueState
		);
	}

	return RepositoryState;
}

FHyperTwistTrainingRepositoryIntegrityReport UHyperTwistContractLibrary::MakeSampleTrainingRepositoryIntegrityReport()
{
	FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	if (RepositoryState.RunRecords.Num() > 0)
	{
		RepositoryState.RunRecords.Add(RepositoryState.RunRecords[0]);
	}
	RepositoryState.RunRecords.Add(FHyperTwistTrainingRunRecord());

	if (RepositoryState.LearnerStates.Num() > 0)
	{
		RepositoryState.LearnerStates.Add(RepositoryState.LearnerStates[0]);
	}
	RepositoryState.LearnerStates.Add(FHyperTwistTrainingCaseLearnerState());

	const FHyperTwistTrainingReviewPlanState ReviewPlanState = MakeSampleTrainingReviewPlanState();
	if (ReviewPlanState.IsStructurallyValid())
	{
		RepositoryState.ReviewPlans.Add(ReviewPlanState);
		RepositoryState.ReviewPlans.Add(ReviewPlanState);
	}
	RepositoryState.ReviewPlans.Add(FHyperTwistTrainingReviewPlanState());

	const TArray<FHyperTwistTrainingSessionTemplate> SessionTemplates = MakeSampleTrainingSessionTemplates();
	if (SessionTemplates.Num() > 0)
	{
		RepositoryState.StoredSessionTemplates.Add(SessionTemplates[0]);
		RepositoryState.StoredSessionTemplates.Add(SessionTemplates[0]);
	}
	RepositoryState.StoredSessionTemplates.Add(FHyperTwistTrainingSessionTemplate());

	const FHyperTwistTrainingMethodDrillFavoriteEntry FavoriteEntry = MakeSampleMethodDrillFavoriteEntry();
	if (FavoriteEntry.IsStructurallyValid())
	{
		RepositoryState.StoredMethodDrillFavorites.Add(FavoriteEntry);
		RepositoryState.StoredMethodDrillFavorites.Add(FavoriteEntry);
	}
	RepositoryState.StoredMethodDrillFavorites.Add(FHyperTwistTrainingMethodDrillFavoriteEntry());

	const FHyperTwistTrainingMethodDrillBatch DrillBatch = MakeSampleMethodDrillBatch();
	if (DrillBatch.IsStructurallyValid())
	{
		RepositoryState.StoredMethodDrillBatches.Add(DrillBatch);
		RepositoryState.StoredMethodDrillBatches.Add(DrillBatch);
	}
	RepositoryState.StoredMethodDrillBatches.Add(FHyperTwistTrainingMethodDrillBatch());

	const FHyperTwistTrainingCoachActionPlan CoachActionPlan = MakeSampleTrainingCoachActionPlan();
	if (CoachActionPlan.IsStructurallyValid())
	{
		RepositoryState.StoredCoachActionPlans.Add(CoachActionPlan);
		RepositoryState.StoredCoachActionPlans.Add(CoachActionPlan);

		FHyperTwistTrainingCoachActionPlan BrokenLineagePlan = CoachActionPlan;
		BrokenLineagePlan.PlanId = TEXT("coach_plan_broken_lineage");
		BrokenLineagePlan.ParentPlanId = TEXT("missing_parent_plan");
		BrokenLineagePlan.RootPlanId = TEXT("missing_root_plan");
		BrokenLineagePlan.SourceSessionId = TEXT("missing_training_session");
		BrokenLineagePlan.SourceReviewPlanId = TEXT("missing_review_plan");
		BrokenLineagePlan.FollowUpDepth = 3;
		RepositoryState.StoredCoachActionPlans.Add(BrokenLineagePlan);

		FHyperTwistTrainingCoachActionPlan CyclicPlan = CoachActionPlan;
		CyclicPlan.PlanId = TEXT("coach_plan_cycle");
		CyclicPlan.ParentPlanId = TEXT("coach_plan_cycle");
		CyclicPlan.RootPlanId = TEXT("coach_plan_cycle");
		CyclicPlan.FollowUpDepth = 2;
		RepositoryState.StoredCoachActionPlans.Add(CyclicPlan);
	}
	RepositoryState.StoredCoachActionPlans.Add(FHyperTwistTrainingCoachActionPlan());

	const FHyperTwistTrainingCoachSessionQueueState CoachSessionQueueState =
		MakeSampleTrainingCoachSessionQueueState();
	if (CoachSessionQueueState.IsStructurallyValid())
	{
		RepositoryState.StoredCoachSessionQueues.Add(CoachSessionQueueState);
		RepositoryState.StoredCoachSessionQueues.Add(CoachSessionQueueState);

		FHyperTwistTrainingCoachSessionQueueState BrokenQueueState = CoachSessionQueueState;
		BrokenQueueState.QueueId = TEXT("coach_queue_broken");
		BrokenQueueState.ActiveEntryId = TEXT("missing_active_entry");
		BrokenQueueState.ActiveTrainingSessionId = TEXT("missing_training_session");
		if (BrokenQueueState.Entries.Num() > 0)
		{
			BrokenQueueState.Entries[0].EntryState = EHyperTwistTrainingCoachSessionQueueEntryState::InProgress;
			BrokenQueueState.Entries[0].SourceTrainingSessionId = TEXT("missing_training_session");
			BrokenQueueState.Entries[0].StartedAtUtc = TEXT("2026-04-28T12:15:00Z");
		}
		RepositoryState.StoredCoachSessionQueues.Add(BrokenQueueState);

		FHyperTwistTrainingCoachSessionQueueHistoryEntry BrokenHistoryEntry;
		BrokenHistoryEntry.HistoryEntryId = TEXT("history_missing_training_session");
		BrokenHistoryEntry.QueueId = BrokenQueueState.QueueId;
		BrokenHistoryEntry.UserId = BrokenQueueState.UserId;
		BrokenHistoryEntry.EntryId = BrokenQueueState.Entries.Num() > 0
			? BrokenQueueState.Entries[0].EntryId
			: TEXT("missing_entry");
		BrokenHistoryEntry.SourceLabel = BrokenQueueState.Entries.Num() > 0
			? BrokenQueueState.Entries[0].SourceLabel
			: TEXT("coach_queue_primary");
		BrokenHistoryEntry.WorkFingerprint = BrokenQueueState.Entries.Num() > 0
			? BrokenQueueState.Entries[0].WorkFingerprint
			: TEXT("broken_history_work");
		BrokenHistoryEntry.EventKind = EHyperTwistTrainingCoachSessionQueueEventKind::Started;
		BrokenHistoryEntry.EventAtUtc = TEXT("2026-04-28T12:15:00Z");
		BrokenHistoryEntry.PreviousEntryState = EHyperTwistTrainingCoachSessionQueueEntryState::Ready;
		BrokenHistoryEntry.NewEntryState = EHyperTwistTrainingCoachSessionQueueEntryState::InProgress;
		BrokenHistoryEntry.SourceTrainingSessionId = TEXT("missing_training_session");
		BrokenHistoryEntry.FocusDeckId = BrokenQueueState.FocusDeckId;
		BrokenHistoryEntry.FocusMethodSegmentId = BrokenQueueState.FocusMethodSegmentId;
		if (BrokenQueueState.Entries.Num() > 0)
		{
			BrokenHistoryEntry.FocusCaseIds = BrokenQueueState.Entries[0].FocusCaseIds;
		}
		RepositoryState.StoredCoachSessionQueueHistory.Add(BrokenHistoryEntry);

		FHyperTwistTrainingCoachSessionQueueHistoryEntry OrphanedQueueHistoryEntry = BrokenHistoryEntry;
		OrphanedQueueHistoryEntry.HistoryEntryId = TEXT("history_missing_queue");
		OrphanedQueueHistoryEntry.QueueId = TEXT("missing_queue");
		RepositoryState.StoredCoachSessionQueueHistory.Add(OrphanedQueueHistoryEntry);

		FHyperTwistTrainingCoachSessionQueueHistoryEntry OrphanedEntryHistoryEntry = BrokenHistoryEntry;
		OrphanedEntryHistoryEntry.HistoryEntryId = TEXT("history_missing_entry_link");
		OrphanedEntryHistoryEntry.QueueId = CoachSessionQueueState.QueueId;
		OrphanedEntryHistoryEntry.EntryId = TEXT("missing_entry_link");
		RepositoryState.StoredCoachSessionQueueHistory.Add(OrphanedEntryHistoryEntry);
	}
	RepositoryState.StoredCoachSessionQueues.Add(FHyperTwistTrainingCoachSessionQueueState());

	return UHyperTwistTrainingRepositoryLibrary::DeriveRepositoryIntegrityReport(RepositoryState);
}

FHyperTwistTrainingDeckHistorySummary UHyperTwistContractLibrary::MakeSampleTrainingDeckHistorySummary()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingDeckHistorySummary();
	}

	const FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	return UHyperTwistTrainingRepositoryLibrary::DeriveDeckHistorySummary(
		RepositoryState,
		StepResult.UpdatedRunState.Session.UserId,
		StepResult.UpdatedRunState.Session.DeckId
	);
}

FHyperTwistTrainingTimingTrendSummary UHyperTwistContractLibrary::MakeSampleTrainingTimingTrendSummary()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingTimingTrendSummary();
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveTimingTrendSummary(
		MakeSampleTrainingRepositoryState(),
		StepResult.UpdatedRunState.Session.UserId,
		StepResult.UpdatedRunState.Session.DeckId,
		TEXT("2026-04-28T12:05:00Z")
	);
}

FHyperTwistTrainingPersonalBestSummary UHyperTwistContractLibrary::MakeSampleTrainingPersonalBestSummary()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingPersonalBestSummary();
	}

	return UHyperTwistTrainingRepositoryLibrary::DerivePersonalBestSummary(
		MakeSampleTrainingRepositoryState(),
		StepResult.UpdatedRunState.Session.UserId,
		StepResult.UpdatedRunState.Session.DeckId,
		TEXT("2026-04-28T12:05:00Z")
	);
}

FHyperTwistTrainingTimerExportPacket UHyperTwistContractLibrary::MakeSampleTrainingTimerExportPacket()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingTimerExportPacket();
	}

	return UHyperTwistTrainingRepositoryLibrary::ExportTimerResultsForDeck(
		MakeSampleTrainingRepositoryState(),
		StepResult.UpdatedRunState.Session.UserId,
		StepResult.UpdatedRunState.Session.DeckId,
		TEXT("2026-04-28T12:05:00Z")
	);
}

FHyperTwistTrainingTimerExportIntegrityReport UHyperTwistContractLibrary::MakeSampleTrainingTimerExportIntegrityReport()
{
	FHyperTwistTrainingTimerExportPacket TimerExportPacket = MakeSampleTrainingTimerExportPacket();
	TimerExportPacket.FormatVersion.Reset();
	TimerExportPacket.ExportedAtUtc.Reset();
	if (TimerExportPacket.Entries.Num() > 0)
	{
		TimerExportPacket.Entries.Add(TimerExportPacket.Entries[0]);

		FHyperTwistTrainingTimerExportEntry MismatchedEntry = TimerExportPacket.Entries[0];
		MismatchedEntry.AttemptId += TEXT("-mismatch");
		MismatchedEntry.UserId = TEXT("other-user");
		TimerExportPacket.Entries.Add(MismatchedEntry);
	}
	TimerExportPacket.Entries.Add(FHyperTwistTrainingTimerExportEntry());

	return UHyperTwistTrainingRepositoryLibrary::DeriveTimerExportIntegrityReport(TimerExportPacket);
}

FHyperTwistTrainingCaseScopedStats UHyperTwistContractLibrary::MakeSampleTrainingCaseScopedStats()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingCaseScopedStats();
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveCaseScopedStats(
		MakeSampleTrainingRepositoryState(),
		StepResult.UpdatedRunState.Session.UserId,
		StepResult.UpdatedRunState.Session.DeckId,
		StepResult.AttemptResolution.UpdatedProgressionMemory.CaseId,
		TEXT("2026-04-28T12:05:00Z")
	);
}

FHyperTwistTrainingDeckScopedStats UHyperTwistContractLibrary::MakeSampleTrainingDeckScopedStats()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingDeckScopedStats();
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveDeckScopedStats(
		MakeSampleTrainingRepositoryState(),
		StepResult.UpdatedRunState.Session.UserId,
		StepResult.UpdatedRunState.Session.DeckId,
		TEXT("2026-04-28T12:05:00Z")
	);
}

FHyperTwistTrainingCaseLearnerState UHyperTwistContractLibrary::MakeSampleTrainingCaseLearnerState()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid() || !StepResult.AttemptResolution.NextCaseSelection.IsStructurallyValid())
	{
		return FHyperTwistTrainingCaseLearnerState();
	}

	const FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	FHyperTwistTrainingCaseLearnerState LearnerState;
	if (UHyperTwistTrainingRepositoryLibrary::TryGetLearnerState(
		RepositoryState,
		StepResult.UpdatedRunState.Session.UserId,
		StepResult.UpdatedRunState.Session.DeckId,
		StepResult.AttemptResolution.UpdatedProgressionMemory.CaseId,
		LearnerState
	))
	{
		return LearnerState;
	}

	return FHyperTwistTrainingCaseLearnerState();
}

FHyperTwistTrainingLearnerDeckStateSummary UHyperTwistContractLibrary::MakeSampleTrainingLearnerDeckStateSummary()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingLearnerDeckStateSummary();
	}

	const FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	return UHyperTwistTrainingRepositoryLibrary::DeriveLearnerDeckStateSummary(
		RepositoryState,
		StepResult.UpdatedRunState.Session.UserId,
		StepResult.UpdatedRunState.Session.DeckId,
		TEXT("2026-04-28T12:05:00Z")
	);
}

FHyperTwistTrainingLearnerProfile UHyperTwistContractLibrary::MakeSampleTrainingLearnerProfile()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingLearnerProfile();
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveLearnerProfile(
		MakeSampleTrainingRepositoryState(),
		StepResult.UpdatedRunState.Session.UserId,
		TEXT("2026-04-28T12:05:00Z")
	);
}

FHyperTwistTrainingCoachMemorySnapshot UHyperTwistContractLibrary::MakeSampleTrainingCoachMemorySnapshot()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingCoachMemorySnapshot();
	}

	FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	FHyperTwistCoachSessionQueueSummary QueueSummary;
	QueueSummary.UserId = StepResult.UpdatedRunState.Session.UserId;
	QueueSummary.ReferenceUtc = TEXT("2026-04-28T12:12:00Z");
	QueueSummary.FocusDeckId = StepResult.UpdatedRunState.Session.DeckId;
	QueueSummary.FocusMethodSegmentId = TEXT("cfop-cross");
	QueueSummary.PrimaryQueueLabel = TEXT("primary-brief");
	QueueSummary.NextQueueLabel = TEXT("follow-up-brief");
	QueueSummary.bHasImmediateQueue = true;
	QueueSummary.bNeedsArchiveAwareSequencing = true;

	FHyperTwistCoachSessionQueueEntry PrimaryEntry;
	PrimaryEntry.EntryId = TEXT("coach_queue_primary");
	PrimaryEntry.SourceLabel = TEXT("primary-brief");
	PrimaryEntry.Priority = EHyperTwistCoachPriority::Elevated;
	PrimaryEntry.PriorityScore = 78.0f;
	PrimaryEntry.Headline = TEXT("Primary coach review block");
	PrimaryEntry.SuggestedMode = EHyperTwistTrainingDeliveryMode::CoachReviewed;
	PrimaryEntry.SuggestedSelectionPolicy = EHyperTwistTrainingSelectionPolicy::Weighted;
	PrimaryEntry.FocusDeckId = QueueSummary.FocusDeckId;
	PrimaryEntry.FocusMethodSegmentId = QueueSummary.FocusMethodSegmentId;
	PrimaryEntry.FocusCaseIds = { TEXT("cross_case_1"), TEXT("cross_case_2") };
	QueueSummary.Entries.Add(PrimaryEntry);

	FHyperTwistCoachSessionQueueEntry FollowUpEntry;
	FollowUpEntry.EntryId = TEXT("coach_queue_follow_up");
	FollowUpEntry.SourceLabel = TEXT("follow-up-brief");
	FollowUpEntry.Priority = EHyperTwistCoachPriority::Advisory;
	FollowUpEntry.PriorityScore = 61.0f;
	FollowUpEntry.Headline = TEXT("Follow-up coaching block");
	FollowUpEntry.SuggestedMode = EHyperTwistTrainingDeliveryMode::VirtualCube;
	FollowUpEntry.SuggestedSelectionPolicy = EHyperTwistTrainingSelectionPolicy::Scripted;
	FollowUpEntry.FocusDeckId = QueueSummary.FocusDeckId;
	FollowUpEntry.FocusMethodSegmentId = QueueSummary.FocusMethodSegmentId;
	FollowUpEntry.FocusCaseIds = { TEXT("cross_case_2"), TEXT("cross_case_3") };
	FollowUpEntry.bRequiresFollowUp = true;
	QueueSummary.Entries.Add(FollowUpEntry);

	FHyperTwistCoachSessionQueueEntry ArchiveEntry;
	ArchiveEntry.EntryId = TEXT("coach_queue_archive");
	ArchiveEntry.SourceLabel = TEXT("archive-policy");
	ArchiveEntry.Priority = EHyperTwistCoachPriority::Advisory;
	ArchiveEntry.PriorityScore = 56.0f;
	ArchiveEntry.Headline = TEXT("Archive retention review block");
	ArchiveEntry.SuggestedMode = EHyperTwistTrainingDeliveryMode::Timer;
	ArchiveEntry.SuggestedSelectionPolicy = EHyperTwistTrainingSelectionPolicy::Weighted;
	ArchiveEntry.FocusDeckId = QueueSummary.FocusDeckId;
	ArchiveEntry.FocusMethodSegmentId = QueueSummary.FocusMethodSegmentId;
	ArchiveEntry.FocusCaseIds = { TEXT("cross_case_4") };
	ArchiveEntry.bRequiresArchiveRetentionTuning = true;
	QueueSummary.Entries.Add(ArchiveEntry);

	FHyperTwistTrainingCoachSessionQueueState QueueState =
		UHyperTwistTrainingRepositoryLibrary::BuildCoachSessionQueueState(
			QueueSummary,
			TEXT("coach_queue_active"),
			TEXT("2026-04-28T12:12:00Z")
		);
	RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachSessionQueueState(
		RepositoryState,
		QueueState
	);
	QueueState = UHyperTwistTrainingRepositoryLibrary::DeferCoachSessionQueueEntry(
		QueueState,
		TEXT("coach_queue_follow_up"),
		TEXT("2026-04-28T18:00:00Z"),
		TEXT("follow-up-held-behind-primary"),
		false,
		TEXT("2026-04-28T12:13:00Z")
	);
	RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachSessionQueueState(
		RepositoryState,
		QueueState
	);
	QueueState = UHyperTwistTrainingRepositoryLibrary::DeferCoachSessionQueueEntry(
		QueueState,
		TEXT("coach_queue_follow_up"),
		TEXT("2026-04-28T21:00:00Z"),
		TEXT("manual-reschedule"),
		true,
		TEXT("2026-04-28T12:18:00Z")
	);
	RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachSessionQueueState(
		RepositoryState,
		QueueState
	);
	QueueState = UHyperTwistTrainingRepositoryLibrary::DeferCoachSessionQueueEntry(
		QueueState,
		TEXT("coach_queue_archive"),
		TEXT("2026-04-29T09:00:00Z"),
		TEXT("archive-retention-window"),
		true,
		TEXT("2026-04-28T12:20:00Z")
	);
	RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachSessionQueueState(
		RepositoryState,
		QueueState
	);
	QueueState = UHyperTwistTrainingRepositoryLibrary::StartCoachSessionQueueEntryBySourceLabel(
		QueueState,
		TEXT("primary-brief"),
		TEXT("coach_training_session_queued_01"),
		TEXT("2026-04-28T12:22:00Z"),
		FString()
	);
	RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachSessionQueueState(
		RepositoryState,
		QueueState
	);
	QueueState = UHyperTwistTrainingRepositoryLibrary::FinalizeCoachSessionQueueEntry(
		QueueState,
		TEXT("coach_training_session_queued_01"),
		TEXT("2026-04-28T12:35:00Z"),
		false
	);
	RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachSessionQueueState(
		RepositoryState,
		QueueState
	);

	return UHyperTwistTrainingRepositoryLibrary::DeriveCoachMemorySnapshot(
		RepositoryState,
		StepResult.UpdatedRunState.Session.UserId,
		TEXT("2026-04-28T12:40:00Z")
	);
}

TArray<FHyperTwistTrainingMethodSegment> UHyperTwistContractLibrary::MakeSampleTrainingMethodSegments()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return TArray<FHyperTwistTrainingMethodSegment>();
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveLearnerMethodSegments(
		MakeSampleTrainingRepositoryState(),
		StepResult.UpdatedRunState.Session.UserId,
		TEXT("2026-04-28T12:05:00Z")
	);
}

TArray<FHyperTwistTrainingLearnerPreset> UHyperTwistContractLibrary::MakeSampleTrainingLearnerPresets()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return TArray<FHyperTwistTrainingLearnerPreset>();
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveLearnerPresets(
		MakeSampleTrainingRepositoryState(),
		StepResult.UpdatedRunState.Session.UserId,
		TEXT("2026-04-28T12:05:00Z")
	);
}

TArray<FHyperTwistTrainingSessionTemplate> UHyperTwistContractLibrary::MakeSampleTrainingSessionTemplates()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return TArray<FHyperTwistTrainingSessionTemplate>();
	}

	const FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	const TArray<FHyperTwistTrainingSessionTemplate> DerivedTemplates =
		UHyperTwistTrainingRepositoryLibrary::DeriveTrainingSessionTemplates(
		RepositoryState,
		StepResult.UpdatedRunState.Session.UserId,
		TEXT("2026-04-28T12:05:00Z")
	);
	return UHyperTwistTrainingRepositoryLibrary::MergeTrainingSessionTemplates(
		DerivedTemplates,
		RepositoryState,
		StepResult.UpdatedRunState.Session.UserId
	);
}

FHyperTwistTrainingSessionTemplate UHyperTwistContractLibrary::MakeSampleCustomizedTrainingSessionTemplate()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingSessionTemplate();
	}

	const TArray<FHyperTwistTrainingSessionTemplate> StoredTemplates =
		UHyperTwistTrainingRepositoryLibrary::ListStoredTrainingSessionTemplates(
			MakeSampleTrainingRepositoryState(),
			StepResult.UpdatedRunState.Session.UserId
		);
	return StoredTemplates.Num() > 0 ? StoredTemplates[0] : FHyperTwistTrainingSessionTemplate();
}

FHyperTwistTrainingDeck UHyperTwistContractLibrary::MakeSampleTrainingSessionTemplateDeck()
{
	const TArray<FHyperTwistTrainingSessionTemplate> Templates = MakeSampleTrainingSessionTemplates();
	if (Templates.Num() == 0)
	{
		return FHyperTwistTrainingDeck();
	}

	return UHyperTwistTrainingCatalogLibrary::MaterializeTrainingSessionTemplateDeck(
		Templates[0],
		UHyperTwistTrainingCatalogLibrary::MakePhase3TrainingCatalog()
	);
}

TArray<FHyperTwistTrainingCaseRecommendation> UHyperTwistContractLibrary::MakeSampleTrainingCaseRecommendations()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return TArray<FHyperTwistTrainingCaseRecommendation>();
	}

	const FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	return UHyperTwistTrainingRepositoryLibrary::ListCaseRecommendationsForDeck(
		RepositoryState,
		StepResult.UpdatedRunState.ActiveDeck,
		StepResult.UpdatedRunState.Session.UserId,
		TEXT("2026-04-28T12:05:00Z"),
		5
	);
}

FHyperTwistTrainingDeck UHyperTwistContractLibrary::MakeSampleTrainingReviewDeck()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingDeck();
	}

	const TArray<FHyperTwistTrainingCaseRecommendation> Recommendations = MakeSampleTrainingCaseRecommendations();
	return UHyperTwistTrainingRepositoryLibrary::BuildReviewDeckFromRecommendations(
		StepResult.UpdatedRunState.ActiveDeck,
		Recommendations,
		2
	);
}

FHyperTwistTrainingReviewPlanState UHyperTwistContractLibrary::MakeSampleTrainingReviewPlanState()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingReviewPlanState();
	}

	const TArray<FHyperTwistTrainingCaseRecommendation> Recommendations = MakeSampleTrainingCaseRecommendations();
	const FHyperTwistTrainingDeck ReviewDeck = MakeSampleTrainingReviewDeck();
	const FHyperTwistTrainingReviewPolicy ReviewPolicy =
		UHyperTwistTrainingRepositoryLibrary::GetDefaultReviewPolicy(EHyperTwistTrainingDeliveryMode::Timer);
	return UHyperTwistTrainingRepositoryLibrary::BuildReviewPlan(
		StepResult.UpdatedRunState.ActiveDeck,
		ReviewDeck,
		StepResult.UpdatedRunState.Session.UserId,
		Recommendations,
		ReviewPolicy,
		TEXT("review_plan_training_session_01"),
		TEXT("2026-04-28T12:05:00Z"),
		EHyperTwistTrainingDeliveryMode::Timer
	);
}

FHyperTwistTrainingReviewFlowStatus UHyperTwistContractLibrary::MakeSampleTrainingReviewFlowStatus()
{
	return UHyperTwistTrainingRepositoryLibrary::DeriveReviewFlowStatus(
		MakeSampleTrainingReviewPlanState()
	);
}

FHyperTwistTrainingReviewProgramSummary UHyperTwistContractLibrary::MakeSampleTrainingReviewProgramSummary()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingReviewProgramSummary();
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveReviewProgramSummary(
		MakeSampleTrainingRepositoryState(),
		StepResult.UpdatedRunState.Session.UserId,
		TEXT("2026-04-28T12:05:00Z")
	);
}

FHyperTwistReplayReviewAnalytics UHyperTwistContractLibrary::MakeSampleReplayReviewAnalytics()
{
	return UHyperTwistReplayReviewLibrary::DeriveReplayReviewAnalytics(
		MakeSampleRuntimeReplayPacket()
	);
}

FHyperTwistRecognitionReplaySummary UHyperTwistContractLibrary::MakeSampleRecognitionReplaySummary()
{
	return UHyperTwistRecognitionReplayLibrary::DeriveRecognitionReplaySummary(
		MakeSampleRecognitionReplayPacket()
	);
}

TArray<FHyperTwistCoachSignal> UHyperTwistContractLibrary::MakeSampleTrainingCoachSignals()
{
	return UHyperTwistTrainingCoachLibrary::DeriveCoachSignals(
		MakeSampleTrainingRunSummary(),
		MakeSampleTrainingDeckScopedStats(),
		MakeSampleTrainingLearnerDeckStateSummary(),
		MakeSampleTrainingLearnerProfile(),
		MakeSampleTrainingCoachMemorySnapshot(),
		MakeSampleTrainingReviewProgramSummary(),
		MakeSampleReplayReviewAnalytics(),
		MakeSampleRecognitionReplaySummary()
	);
}

FHyperTwistCoachBrief UHyperTwistContractLibrary::MakeSampleTrainingCoachBrief()
{
	return UHyperTwistTrainingCoachLibrary::DeriveCoachBrief(
		MakeSampleTrainingRunSummary(),
		MakeSampleTrainingDeckScopedStats(),
		MakeSampleTrainingLearnerDeckStateSummary(),
		MakeSampleTrainingLearnerProfile(),
		MakeSampleTrainingCoachMemorySnapshot(),
		MakeSampleTrainingReviewProgramSummary(),
		MakeSampleReplayReviewAnalytics(),
		MakeSampleRecognitionReplaySummary()
	);
}

FHyperTwistTrainingCoachActionPlan UHyperTwistContractLibrary::MakeSampleTrainingCoachActionPlan()
{
	return UHyperTwistTrainingRepositoryLibrary::BuildCoachActionPlan(
		MakeSampleTrainingCoachBrief(),
		MakeSampleTrainingCoachMemorySnapshot(),
		TEXT("coach_plan_training_session_01"),
		TEXT("2026-04-28T12:05:00Z"),
		0,
		0,
		FString(),
		MakeSampleTrainingCoachBrief().FocusCaseIds
	);
}

FHyperTwistTrainingCoachActionPlanSummary UHyperTwistContractLibrary::MakeSampleTrainingCoachActionPlanSummary()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingCoachActionPlanSummary();
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveCoachActionPlanSummary(
		MakeSampleTrainingRepositoryState(),
		StepResult.UpdatedRunState.Session.UserId,
		TEXT("2026-04-28T12:05:00Z")
	);
}

FHyperTwistTrainingCoachActionPlanOutcomeSummary UHyperTwistContractLibrary::MakeSampleTrainingCoachActionPlanOutcomeSummary()
{
	return UHyperTwistTrainingRepositoryLibrary::DeriveCoachActionPlanOutcomeSummary(
		MakeSampleTrainingRepositoryState(),
		MakeSampleTrainingCoachActionPlan(),
		TEXT("2026-04-28T12:12:00Z")
	);
}

FHyperTwistTrainingCoachActionSequenceSummary UHyperTwistContractLibrary::MakeSampleTrainingCoachActionSequenceSummary()
{
	FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	const FHyperTwistTrainingCoachActionPlan RootPlan = MakeSampleTrainingCoachActionPlan();
	const FHyperTwistCoachBrief FollowUpBrief = MakeSampleTrainingCoachFollowUpBrief();
	const FHyperTwistTrainingCoachActionPlan FollowUpPlan =
		UHyperTwistTrainingRepositoryLibrary::BuildCoachActionPlan(
			FollowUpBrief,
			MakeSampleTrainingCoachMemorySnapshot(),
			TEXT("coach_follow_up_training_session_02"),
			TEXT("2026-04-28T12:20:00Z"),
			0,
			0,
			FString(),
			FollowUpBrief.FocusCaseIds
		);
	if (FollowUpPlan.IsStructurallyValid())
	{
		RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachActionPlan(
			RepositoryState,
			FollowUpPlan
		);
		return UHyperTwistTrainingRepositoryLibrary::DeriveCoachActionSequenceSummary(
			RepositoryState,
			FollowUpPlan,
			TEXT("2026-04-28T12:20:00Z")
		);
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveCoachActionSequenceSummary(
		RepositoryState,
		RootPlan,
		TEXT("2026-04-28T12:20:00Z")
	);
}

FHyperTwistTrainingCoachActionClosureSummary UHyperTwistContractLibrary::MakeSampleTrainingCoachActionClosureSummary()
{
	FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	const FHyperTwistTrainingCoachActionPlan RootPlan = MakeSampleTrainingCoachActionPlan();
	const FHyperTwistCoachBrief FollowUpBrief = MakeSampleTrainingCoachFollowUpBrief();
	const FHyperTwistTrainingCoachActionPlan FollowUpPlan =
		UHyperTwistTrainingRepositoryLibrary::BuildCoachActionPlan(
			FollowUpBrief,
			MakeSampleTrainingCoachMemorySnapshot(),
			TEXT("coach_follow_up_training_session_02"),
			TEXT("2026-04-28T12:20:00Z"),
			0,
			0,
			FString(),
			FollowUpBrief.FocusCaseIds
		);
	if (FollowUpPlan.IsStructurallyValid())
	{
		RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachActionPlan(
			RepositoryState,
			FollowUpPlan
		);
		return UHyperTwistTrainingRepositoryLibrary::DeriveCoachActionClosureSummary(
			RepositoryState,
			FollowUpPlan,
			TEXT("2026-04-28T12:20:00Z")
		);
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveCoachActionClosureSummary(
		RepositoryState,
		RootPlan,
		TEXT("2026-04-28T12:20:00Z")
	);
}

FHyperTwistTrainingCoachClosureMemorySummary UHyperTwistContractLibrary::MakeSampleTrainingCoachClosureMemorySummary()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingCoachClosureMemorySummary();
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveCoachClosureMemorySummary(
		MakeSampleTrainingRepositoryState(),
		StepResult.UpdatedRunState.Session.UserId,
		TEXT("2026-04-28T12:20:00Z")
	);
}

FHyperTwistTrainingCoachRecommendationHistorySummary UHyperTwistContractLibrary::MakeSampleTrainingCoachRecommendationHistorySummary()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingCoachRecommendationHistorySummary();
	}

	FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	const FHyperTwistCoachBrief FollowUpBrief = MakeSampleTrainingCoachFollowUpBrief();
	const FHyperTwistTrainingCoachActionPlan FollowUpPlan =
		UHyperTwistTrainingRepositoryLibrary::BuildCoachActionPlan(
			FollowUpBrief,
			MakeSampleTrainingCoachMemorySnapshot(),
			TEXT("coach_follow_up_training_session_02"),
			TEXT("2026-04-28T12:20:00Z"),
			0,
			0,
			FString(),
			FollowUpBrief.FocusCaseIds
		);
	if (FollowUpPlan.IsStructurallyValid())
	{
		RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachActionPlan(
			RepositoryState,
			FollowUpPlan
		);
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveCoachRecommendationHistorySummary(
		RepositoryState,
		StepResult.UpdatedRunState.Session.UserId,
		TEXT("2026-04-28T12:20:00Z")
	);
}

FHyperTwistTrainingCoachCarryForwardPolicySummary UHyperTwistContractLibrary::MakeSampleTrainingCoachCarryForwardPolicySummary()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingCoachCarryForwardPolicySummary();
	}

	FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	const FHyperTwistCoachBrief FollowUpBrief = MakeSampleTrainingCoachFollowUpBrief();
	const FHyperTwistTrainingCoachActionPlan FollowUpPlan =
		UHyperTwistTrainingRepositoryLibrary::BuildCoachActionPlan(
			FollowUpBrief,
			MakeSampleTrainingCoachMemorySnapshot(),
			TEXT("coach_follow_up_training_session_02"),
			TEXT("2026-04-28T12:20:00Z"),
			0,
			0,
			FString(),
			FollowUpBrief.FocusCaseIds
		);
	if (FollowUpPlan.IsStructurallyValid())
	{
		RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachActionPlan(
			RepositoryState,
			FollowUpPlan
		);
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveCoachCarryForwardPolicySummary(
		RepositoryState,
		StepResult.UpdatedRunState.Session.UserId,
		TEXT("2026-04-28T12:20:00Z")
	);
}

FHyperTwistTrainingCoachArchivePolicySummary UHyperTwistContractLibrary::MakeSampleTrainingCoachArchivePolicySummary()
{
	const FHyperTwistTrainingRunStepResult StepResult = MakeSampleTrainingRunStepResult();
	if (!StepResult.IsStructurallyValid())
	{
		return FHyperTwistTrainingCoachArchivePolicySummary();
	}

	FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	const FHyperTwistCoachBrief FollowUpBrief = MakeSampleTrainingCoachFollowUpBrief();
	const FHyperTwistTrainingCoachActionPlan FollowUpPlan =
		UHyperTwistTrainingRepositoryLibrary::BuildCoachActionPlan(
			FollowUpBrief,
			MakeSampleTrainingCoachMemorySnapshot(),
			TEXT("coach_follow_up_training_session_02"),
			TEXT("2026-04-28T12:20:00Z"),
			0,
			0,
			FString(),
			FollowUpBrief.FocusCaseIds
		);
	if (FollowUpPlan.IsStructurallyValid())
	{
		RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachActionPlan(
			RepositoryState,
			FollowUpPlan
		);
	}

	return UHyperTwistTrainingRepositoryLibrary::DeriveCoachArchivePolicySummary(
		RepositoryState,
		StepResult.UpdatedRunState.Session.UserId,
		TEXT("2026-04-28T12:20:00Z")
	);
}

FHyperTwistCoachBrief UHyperTwistContractLibrary::MakeSampleTrainingCoachFollowUpBrief()
{
	return UHyperTwistTrainingCoachLibrary::DeriveCoachFollowUpBrief(
		MakeSampleTrainingCoachBrief(),
		MakeSampleTrainingCoachActionPlan(),
		MakeSampleTrainingCoachActionPlanOutcomeSummary()
	);
}

FHyperTwistCoachSessionQueueSummary UHyperTwistContractLibrary::MakeSampleTrainingCoachSessionQueueSummary()
{
	return UHyperTwistTrainingCoachLibrary::DeriveCoachSessionQueueSummary(
		MakeSampleTrainingCoachBrief(),
		MakeSampleTrainingCoachFollowUpBrief(),
		MakeSampleTrainingCoachMemorySnapshot(),
		MakeSampleTrainingCoachArchivePolicySummary(),
		MakeSampleTrainingReviewProgramSummary()
	);
}

FHyperTwistTrainingCoachSessionQueueState UHyperTwistContractLibrary::MakeSampleTrainingCoachSessionQueueState()
{
	FHyperTwistTrainingCoachSessionQueueState QueueState = UHyperTwistTrainingRepositoryLibrary::BuildCoachSessionQueueState(
		MakeSampleTrainingCoachSessionQueueSummary(),
		TEXT("coach_queue_active"),
		TEXT("2026-04-28T12:12:00Z")
	);

	QueueState = UHyperTwistTrainingRepositoryLibrary::DeferCoachSessionQueueEntry(
		QueueState,
		TEXT("coach_queue_follow_up"),
		TEXT("2026-04-28T18:00:00Z"),
		TEXT("follow-up-held-behind-primary"),
		false,
		TEXT("2026-04-28T12:13:00Z")
	);
	QueueState = UHyperTwistTrainingRepositoryLibrary::DeferCoachSessionQueueEntry(
		QueueState,
		TEXT("coach_queue_archive"),
		TEXT("2026-04-29T09:00:00Z"),
		TEXT("archive-retention-window"),
		true,
		TEXT("2026-04-28T12:14:00Z")
	);
	return QueueState;
}

FHyperTwistCoachScheduleHorizonSummary UHyperTwistContractLibrary::MakeSampleTrainingCoachScheduleHorizonSummary()
{
	return UHyperTwistTrainingCoachLibrary::DeriveCoachScheduleHorizonSummary(
		MakeSampleTrainingCoachSessionQueueState(),
		MakeSampleTrainingCoachArchivePolicySummary(),
		TEXT("2026-04-28T12:12:00Z")
	);
}

FHyperTwistCoachSchedulePolicySummary UHyperTwistContractLibrary::MakeSampleTrainingCoachSchedulePolicySummary()
{
	return UHyperTwistTrainingCoachLibrary::DeriveCoachSchedulePolicySummary(
		MakeSampleTrainingCoachSessionQueueState(),
		MakeSampleTrainingCoachScheduleHorizonSummary(),
		MakeSampleTrainingCoachActionClosureSummary(),
		UHyperTwistTrainingRepositoryLibrary::DeriveCoachMemorySnapshot(
			MakeSampleTrainingRepositoryState(),
			TEXT("local-user"),
			TEXT("2026-04-28T12:30:00Z")
		),
		MakeSampleTrainingCoachArchivePolicySummary(),
		TEXT("2026-04-28T12:30:00Z")
	);
}

FHyperTwistTrainingCoachSessionQueueExecutionSummary UHyperTwistContractLibrary::MakeSampleTrainingCoachSessionQueueExecutionSummary()
{
	FHyperTwistTrainingRepositoryState RepositoryState = MakeSampleTrainingRepositoryState();
	FHyperTwistTrainingCoachSessionQueueState QueueState =
		UHyperTwistTrainingRepositoryLibrary::BuildCoachSessionQueueState(
			MakeSampleTrainingCoachSessionQueueSummary(),
			TEXT("coach_queue_active"),
			TEXT("2026-04-28T12:12:00Z")
		);
	RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachSessionQueueState(
		RepositoryState,
		QueueState
	);
	QueueState = UHyperTwistTrainingRepositoryLibrary::DeferCoachSessionQueueEntry(
		QueueState,
		TEXT("coach_queue_follow_up"),
		TEXT("2026-04-28T18:00:00Z"),
		TEXT("follow-up-held-behind-primary"),
		false,
		TEXT("2026-04-28T12:13:00Z")
	);
	RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachSessionQueueState(
		RepositoryState,
		QueueState
	);
	QueueState = UHyperTwistTrainingRepositoryLibrary::DeferCoachSessionQueueEntry(
		QueueState,
		TEXT("coach_queue_archive"),
		TEXT("2026-04-29T09:00:00Z"),
		TEXT("archive-retention-window"),
		true,
		TEXT("2026-04-28T12:14:00Z")
	);
	RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachSessionQueueState(
		RepositoryState,
		QueueState
	);
	QueueState = UHyperTwistTrainingRepositoryLibrary::StartCoachSessionQueueEntryBySourceLabel(
		QueueState,
		TEXT("primary-brief"),
		TEXT("coach_training_session_queued_01"),
		TEXT("2026-04-28T12:20:00Z"),
		FString()
	);
	RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachSessionQueueState(
		RepositoryState,
		QueueState
	);
	QueueState = UHyperTwistTrainingRepositoryLibrary::FinalizeCoachSessionQueueEntry(
		QueueState,
		TEXT("coach_training_session_queued_01"),
		TEXT("2026-04-28T12:34:00Z"),
		false
	);
	RepositoryState = UHyperTwistTrainingRepositoryLibrary::UpsertCoachSessionQueueState(
		RepositoryState,
		QueueState
	);

	return UHyperTwistTrainingRepositoryLibrary::DeriveCoachSessionQueueExecutionSummary(
		RepositoryState,
		TEXT("local-user"),
		TEXT("coach_queue_active"),
		TEXT("2026-04-28T12:40:00Z")
	);
}

bool UHyperTwistContractLibrary::IsPuzzleStateStructurallyValid(const FHyperTwistPuzzleState& State)
{
	return State.IsStructurallyValid();
}

bool UHyperTwistContractLibrary::IsReplayPacketStructurallyValid(const FHyperTwistReplayPacket& Packet)
{
	return Packet.IsStructurallyValid();
}

FString UHyperTwistContractLibrary::SerializePuzzleStateToJson(const FHyperTwistPuzzleState& State)
{
	return HyperTwistContractLibraryInternal::SerializeStructToJson(State);
}

FString UHyperTwistContractLibrary::SerializeReplayPacketToJson(const FHyperTwistReplayPacket& Packet)
{
	return HyperTwistContractLibraryInternal::SerializeStructToJson(Packet);
}

FString UHyperTwistContractLibrary::SerializeContentPackToJson(const FHyperTwistContentPack& ContentPack)
{
	return HyperTwistContractLibraryInternal::SerializeStructToJson(ContentPack);
}

FString UHyperTwistContractLibrary::SerializeMethodDrillDefinitionToJson(
	const FHyperTwistTrainingMethodDrillDefinition& DrillDefinition
)
{
	return HyperTwistContractLibraryInternal::SerializeStructToJson(DrillDefinition);
}

FString UHyperTwistContractLibrary::SerializeMethodDrillRunStateToJson(
	const FHyperTwistTrainingMethodDrillRunState& RunState
)
{
	return HyperTwistContractLibraryInternal::SerializeStructToJson(RunState);
}

FString UHyperTwistContractLibrary::SerializeMethodDrillFavoriteEntryToJson(
	const FHyperTwistTrainingMethodDrillFavoriteEntry& FavoriteEntry
)
{
	return HyperTwistContractLibraryInternal::SerializeStructToJson(FavoriteEntry);
}

FString UHyperTwistContractLibrary::SerializeMethodDrillBatchToJson(
	const FHyperTwistTrainingMethodDrillBatch& DrillBatch
)
{
	return HyperTwistContractLibraryInternal::SerializeStructToJson(DrillBatch);
}

FString UHyperTwistContractLibrary::SerializeTrainingRepositoryStateToJson(const FHyperTwistTrainingRepositoryState& RepositoryState)
{
	return HyperTwistContractLibraryInternal::SerializeStructToJson(RepositoryState);
}

FString UHyperTwistContractLibrary::SerializeTrainingTimerExportPacketToJson(
	const FHyperTwistTrainingTimerExportPacket& TimerExportPacket
)
{
	return HyperTwistContractLibraryInternal::SerializeStructToJson(TimerExportPacket);
}

bool UHyperTwistContractLibrary::DeserializePuzzleStateFromJson(const FString& Json, FHyperTwistPuzzleState& OutState)
{
	return HyperTwistContractLibraryInternal::DeserializeStructFromJson(Json, OutState);
}

bool UHyperTwistContractLibrary::DeserializeReplayPacketFromJson(const FString& Json, FHyperTwistReplayPacket& OutPacket)
{
	return HyperTwistContractLibraryInternal::DeserializeStructFromJson(Json, OutPacket);
}

bool UHyperTwistContractLibrary::DeserializeContentPackFromJson(const FString& Json, FHyperTwistContentPack& OutContentPack)
{
	return HyperTwistContractLibraryInternal::DeserializeStructFromJson(Json, OutContentPack);
}

bool UHyperTwistContractLibrary::DeserializeMethodDrillDefinitionFromJson(
	const FString& Json,
	FHyperTwistTrainingMethodDrillDefinition& OutDrillDefinition
)
{
	return HyperTwistContractLibraryInternal::DeserializeStructFromJson(Json, OutDrillDefinition);
}

bool UHyperTwistContractLibrary::DeserializeMethodDrillRunStateFromJson(
	const FString& Json,
	FHyperTwistTrainingMethodDrillRunState& OutRunState
)
{
	return HyperTwistContractLibraryInternal::DeserializeStructFromJson(Json, OutRunState);
}

bool UHyperTwistContractLibrary::DeserializeMethodDrillFavoriteEntryFromJson(
	const FString& Json,
	FHyperTwistTrainingMethodDrillFavoriteEntry& OutFavoriteEntry
)
{
	return HyperTwistContractLibraryInternal::DeserializeStructFromJson(Json, OutFavoriteEntry);
}

bool UHyperTwistContractLibrary::DeserializeMethodDrillBatchFromJson(
	const FString& Json,
	FHyperTwistTrainingMethodDrillBatch& OutDrillBatch
)
{
	return HyperTwistContractLibraryInternal::DeserializeStructFromJson(Json, OutDrillBatch);
}

bool UHyperTwistContractLibrary::DeserializeTrainingRepositoryStateFromJson(const FString& Json, FHyperTwistTrainingRepositoryState& OutRepositoryState)
{
	return HyperTwistContractLibraryInternal::DeserializeStructFromJson(Json, OutRepositoryState);
}

bool UHyperTwistContractLibrary::DeserializeTrainingTimerExportPacketFromJson(
	const FString& Json,
	FHyperTwistTrainingTimerExportPacket& OutTimerExportPacket
)
{
	return HyperTwistContractLibraryInternal::DeserializeStructFromJson(Json, OutTimerExportPacket);
}
