#pragma once

#include "CoreMinimal.h"
#include "Subsystems/GameInstanceSubsystem.h"
#include "HyperTwistAlgorithm/HyperTwistAlgorithmTypes.h"
#include "HyperTwistRecognition/HyperTwistRecognitionReplayLibrary.h"
#include "HyperTwistReplay/HyperTwistReplayReviewLibrary.h"
#include "HyperTwistTraining/HyperTwistTrainingCoachLibrary.h"
#include "HyperTwistTraining/HyperTwistTrainingTypes.h"
#include "HyperTwistTrainingSubsystem.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(
	FOnHyperTwistTrainingDrillTimerCompleted,
	const FHyperTwistTrainingDrillTimerCompletionResult&,
	CompletionResult
);

UCLASS(Config = Game, DefaultConfig)
class UNREALHYPERTWIST_API UHyperTwistTrainingSubsystem : public UGameInstanceSubsystem
{
	GENERATED_BODY()

public:
	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|Training|Drill")
	FOnHyperTwistTrainingDrillTimerCompleted OnDrillTimerCompleted;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	bool HasActiveRun() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Drill")
	bool HasActiveMethodDrillRun() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingRunState GetActiveRunState() const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryGetActiveImportedRuntimeSurface(FHyperTwistTrainingImportedRuntimeSurface& OutRuntimeSurface) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryGetActiveImportedRuntimeSelector(
		const FString& SelectorId,
		FHyperTwistTrainingImportedRuntimeSelector& OutSelector
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryGetActiveImportedRuntimeSelectorOptionCases(
		const FString& SelectorId,
		TArray<FHyperTwistTrainingCase>& OutCases
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryGetActiveImportedRuntimeSelectionState(
		FHyperTwistTrainingImportedRuntimeSelectionState& OutSelectionState
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryGetActiveImportedGeneratedModeLaunchConfig(
		FHyperTwistTrainingImportedGeneratedModeLaunchConfig& OutLaunchConfig
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryGetActiveImportedGeneratedModeLaunchRequest(
		FHyperTwistTrainingImportedGeneratedModeLaunchRequest& OutLaunchRequest
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingImportedGeneratedModeLaunchRequest CreateActiveImportedGeneratedModeLaunchRequest(
		const FString& RequestSource
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryExecuteActiveImportedGeneratedModeLaunchRequest(
		FHyperTwistTrainingRunState& OutRunState,
		FString& OutBlockedReason
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingRunState ApplyActiveImportedRuntimeSelectorOption(
		const FString& SelectorId,
		const FString& OptionId
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingRunState ApplyActiveImportedGeneratedModeSelectorChoice(
		const FString& SelectorId,
		const FString& SelectedValue,
		const FString& DisplayValue
	);

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillRunState GetActiveMethodDrillRunState() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingSessionSummary GetActiveRunSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	bool HasActiveLiveTimer() const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingLiveTimerState GetActiveLiveTimerState();

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillSummary GetActiveMethodDrillSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillDiagnosticPacket GetActiveMethodDrillDiagnosticPacket() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingSessionTemplate GetActiveMethodDrillFollowUpTemplate() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingRepositoryState GetTrainingRepositoryState() const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Templates")
	bool RecordActiveRunTemplateLaunchSelectionProvenance(
		const FHyperTwistTrainingTemplateLaunchSelectionProvenance& SelectionProvenance
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryGetLatestGeneratedModeLaunchRequestForDeck(
		const FString& UserId,
		const FString& DeckId,
		FHyperTwistTrainingImportedGeneratedModeLaunchRequest& OutLaunchRequest
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryGetLatestGeneratedModeLaunchRequestForActiveDeck(
		FHyperTwistTrainingImportedGeneratedModeLaunchRequest& OutLaunchRequest
	) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingRepositoryIntegrityReport GetActiveRepositoryIntegrityReport() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingRepositoryRoundTripVerification GetActiveRepositoryRoundTripVerification() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingCoachPanelState GetActiveCoachPanelState() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingDeckHistorySummary GetActiveDeckHistorySummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingTimingTrendSummary GetActiveTimingTrendSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingPersonalBestSummary GetActivePersonalBestSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingDeckScopedStats GetActiveDeckScopedStats() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingCaseScopedStats GetActiveCurrentCaseScopedStats() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingLearnerDeckStateSummary GetActiveLearnerDeckStateSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingLearnerProfile GetActiveLearnerProfile() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Publication")
	FHyperTwistTrainingPublicationBundle GetActivePublicationBundle() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Social")
	FHyperTwistTrainingSocialChallengeBundle GetActiveSocialChallengeBundle() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingCoachMemorySnapshot GetActiveCoachMemorySnapshot() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistReplayReviewAnalytics GetActiveReplayReviewAnalytics() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistRecognitionReplaySummary GetActiveRecognitionReplaySummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Recognition")
	FHyperTwistTrainingRecognitionSessionState GetActiveRecognitionSessionState() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Speech")
	FHyperTwistTrainingCompanionSpeechSessionState GetActiveCompanionSpeechSessionState() const;

#if WITH_AUTOMATION_TESTS
	void RefreshCompanionSpeechServiceHealthForAutomation();
#endif

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	TArray<FHyperTwistCoachSignal> GetActiveCoachSignals() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistCoachBrief GetActiveCoachBrief() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingCoachActionPlan GetActiveCoachActionPlan() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingCoachActionPlanSummary GetActiveCoachActionPlanSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingCoachActionPlanOutcomeSummary GetActiveCoachActionPlanOutcomeSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingCoachActionSequenceSummary GetActiveCoachActionSequenceSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingCoachActionClosureSummary GetActiveCoachActionClosureSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingCoachClosureMemorySummary GetActiveCoachClosureMemorySummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingCoachRecommendationHistorySummary GetActiveCoachRecommendationHistorySummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingCoachCarryForwardPolicySummary GetActiveCoachCarryForwardPolicySummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingCoachArchivePolicySummary GetActiveCoachArchivePolicySummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistCoachBrief GetActiveCoachFollowUpBrief() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistCoachSessionQueueSummary GetActiveCoachSessionQueueSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingCoachSessionQueueState GetActiveCoachSessionQueueState() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistCoachScheduleHorizonSummary GetActiveCoachScheduleHorizonSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistCoachSchedulePolicySummary GetActiveCoachSchedulePolicySummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingCoachSessionQueueExecutionSummary GetActiveCoachSessionQueueExecutionSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	TArray<FHyperTwistTrainingMethodSegment> GetActiveLearnerMethodSegments() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	TArray<FHyperTwistTrainingLearnerPreset> GetActiveLearnerPresets() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	TArray<FHyperTwistTrainingSessionTemplate> GetActiveTrainingSessionTemplates() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	TArray<FHyperTwistTrainingSessionTemplate> GetTrainingSessionTemplatesForUser(
		const FString& UserId,
		const FString& ReferenceUtc
	) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Drill")
	TArray<FHyperTwistTrainingMethodDrillFavoriteEntry> GetMethodDrillFavoritesForUser(
		const FString& UserId,
		const FString& MethodSegmentId
	) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Drill")
	TArray<FHyperTwistTrainingMethodDrillBatch> GetMethodDrillBatchesForUser(
		const FString& UserId,
		const FString& MethodSegmentId
	) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Drill")
	TArray<FHyperTwistTrainingMethodDrillBatch> GetResumableMethodDrillBatchesForUser(
		const FString& UserId,
		const FString& MethodSegmentId
	) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Social")
	TArray<FHyperTwistTrainingFriendLinkRecord> GetFriendLinksForUser(const FString& UserId) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Social")
	TArray<FHyperTwistTrainingNotificationRecord> GetNotificationsForUser(const FString& UserId) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Social")
	TArray<FHyperTwistTrainingChallengeRecord> GetChallengesForUser(const FString& UserId) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillBatchReadiness DeriveMethodDrillBatchReadiness(
		const FHyperTwistTrainingMethodDrillBatch& DrillBatch,
		const FHyperTwistTrainingMethodDrillDefinition& DrillDefinition
	) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillBatch RebuildMethodDrillBatchForResume(
		const FHyperTwistTrainingMethodDrillBatch& DrillBatch,
		const FHyperTwistTrainingMethodDrillDefinition& DrillDefinition
	) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillDefinition BuildFavoriteFirstMethodDrillDefinition(
		const FHyperTwistTrainingMethodDrillDefinition& BaseDefinition,
		const FString& UserId,
		int32 MaxFavoriteCases
	) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	TArray<FHyperTwistTrainingCaseRecommendation> GetActiveCaseRecommendations() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingReviewPlanState GetActiveReviewPlanState() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingReviewFlowStatus GetActiveReviewFlowStatus() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingReviewProgramSummary GetActiveReviewProgramSummary() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingTimerExportPacket BuildTrainingTimerExportPacket(
		const FString& UserId,
		const FString& DeckId,
		const FString& ReferenceUtc
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryGetStoredRunRecord(
		const FString& TrainingSessionId,
		FHyperTwistTrainingRunRecord& OutRunRecord
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryGetStoredLearnerState(
		const FString& UserId,
		const FString& DeckId,
		const FString& CaseId,
		FHyperTwistTrainingCaseLearnerState& OutLearnerState
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryGetStoredReviewPlan(
		const FString& PlanId,
		FHyperTwistTrainingReviewPlanState& OutReviewPlan
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Coach")
	bool TryGetStoredCoachActionPlan(
		const FString& PlanId,
		FHyperTwistTrainingCoachActionPlan& OutActionPlan
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Social")
	bool TryGetStoredFriendLinkRecord(
		const FString& UserId,
		const FString& FriendUserId,
		FHyperTwistTrainingFriendLinkRecord& OutFriendLinkRecord
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Social")
	bool TryGetStoredNotificationRecord(
		const FString& NotificationId,
		FHyperTwistTrainingNotificationRecord& OutNotificationRecord
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Social")
	bool TryGetStoredChallengeRecord(
		const FString& ChallengeId,
		FHyperTwistTrainingChallengeRecord& OutChallengeRecord
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Coach")
	bool TryGetStoredCoachSessionQueueState(
		const FString& UserId,
		const FString& QueueId,
		FHyperTwistTrainingCoachSessionQueueState& OutQueueState
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryGetStoredTrainingSessionTemplate(
		const FString& UserId,
		const FString& TemplateId,
		FHyperTwistTrainingSessionTemplate& OutTemplate
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool SaveTrainingSessionTemplate(const FHyperTwistTrainingSessionTemplate& SessionTemplate);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Social")
	bool SaveFriendLinkRecord(const FHyperTwistTrainingFriendLinkRecord& FriendLinkRecord);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Social")
	bool SaveNotificationRecord(const FHyperTwistTrainingNotificationRecord& NotificationRecord);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Social")
	bool MarkNotificationRead(const FString& NotificationId);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Social")
	bool AcknowledgeNotification(const FString& NotificationId);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Social")
	bool ArchiveNotification(const FString& NotificationId);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Social")
	bool SaveChallengeRecord(const FHyperTwistTrainingChallengeRecord& ChallengeRecord);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	bool SaveMethodDrillFavorite(const FHyperTwistTrainingMethodDrillFavoriteEntry& FavoriteEntry);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	bool RemoveMethodDrillFavorite(
		const FString& UserId,
		const FString& DrillId,
		const FString& CaseId
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	bool TryGetStoredMethodDrillBatch(
		const FString& UserId,
		const FString& BatchId,
		FHyperTwistTrainingMethodDrillBatch& OutBatch
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	bool SaveMethodDrillBatch(const FHyperTwistTrainingMethodDrillBatch& DrillBatch);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Coach")
	bool SaveCoachSessionQueueState(const FHyperTwistTrainingCoachSessionQueueState& QueueState);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Coach")
	bool DeferCoachSessionQueueEntry(
		const FString& EntryId,
		const FString& DeferredUntilUtc,
		const FString& DeferralReasonLabel,
		bool bManualReschedule
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Coach")
	bool PromoteCoachSessionQueueEntry(
		const FString& EntryId,
		const FString& StartActionSourceLabel
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	bool RemoveMethodDrillBatch(
		const FString& UserId,
		const FString& BatchId
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool RemoveTrainingSessionTemplate(
		const FString& UserId,
		const FString& TemplateId
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool SaveTrainingRepositoryStateToDefaultPath(FString& OutResolvedPath) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool LoadTrainingRepositoryStateFromDefaultPath(FString& OutResolvedPath);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingRepositoryRoundTripVerification VerifyTrainingRepositoryRoundTripAtDefaultPath();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool SaveTrainingTimerExportPacketToDefaultPath(
		const FString& UserId,
		const FString& DeckId,
		FString& OutResolvedPath
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool ImportTrainingTimerExportPacket(const FHyperTwistTrainingTimerExportPacket& TimerExportPacket);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool LoadTrainingTimerExportPacketFromFile(
		const FString& UserId,
		const FString& DeckId,
		const FString& ExportPath,
		FString& OutResolvedPath
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryMaterializeActiveTrainingSessionTemplate(
		const FString& TemplateId,
		FHyperTwistTrainingDeck& OutDeck
	) const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool TryMaterializeTrainingSessionTemplateForUser(
		const FString& UserId,
		const FString& TemplateId,
		const FString& ReferenceUtc,
		FHyperTwistTrainingDeck& OutDeck
	) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training")
	FHyperTwistTrainingDeck BuildActiveReviewDeck(int32 MaxCases) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingDeck BuildActiveCoachRecommendedDeck(int32 MaxCases) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingDeck BuildActiveCoachFollowUpDeck(int32 MaxCases) const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingDeck BuildActiveMethodDrillFollowUpDeck() const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingRunState StartTrainingRunFromTemplate(
		const FString& TemplateId,
		const FString& UserId,
		const FString& SessionId
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingRunState StartActiveMethodDrillFollowUpRun(const FString& SessionId);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingRunState StartRecommendedReviewRun(
		const FString& SessionId,
		int32 MaxCases,
		EHyperTwistTrainingDeliveryMode Mode
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingRunState StartCoachRecommendedRun(
		const FString& SessionId,
		int32 MaxCases,
		const FString& StartActionSourceLabel
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingRunState StartCoachFollowUpRun(
		const FString& SessionId,
		int32 MaxCases,
		const FString& StartActionSourceLabel
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingRunState StartPrimaryCoachAction(
		const FString& SessionId,
		int32 MaxCases,
		const FString& StartActionSourceLabel
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingRunState StartNextQueuedCoachRun(
		const FString& SessionId,
		int32 MaxCases,
		const FString& StartActionSourceLabel
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Coach")
	FHyperTwistTrainingRunState StartCoachQueueEntry(
		const FString& EntryId,
		const FString& SessionId,
		int32 MaxCases
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingRunState StartTrainingRunFromDeck(
		const FHyperTwistTrainingDeck& Deck,
		const FString& UserId,
		const FString& SessionId,
		EHyperTwistTrainingDeliveryMode Mode
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingRunState StartTrainingRunFromImportedDeckId(
		const FString& DeckId,
		const FString& UserId,
		const FString& SessionId,
		EHyperTwistTrainingDeliveryMode Mode
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingRunState StartSampleClassicTrainingRun(
		const FString& UserId,
		const FString& SessionId,
		EHyperTwistTrainingDeliveryMode Mode
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillRunState StartMethodDrillRun(
		const FHyperTwistTrainingMethodDrillDefinition& DrillDefinition,
		const FString& UserId,
		const FString& RunId
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillRunState StartMethodDrillRunFromDeck(
		const FHyperTwistTrainingDeck& Deck,
		const FString& UserId,
		const FString& RunId,
		EHyperTwistTrainingDrillModeKind DrillMode,
		EHyperTwistTrainingDrillVisibilityPolicy VisibilityPolicy,
		bool bAnswerHiddenUntilReveal
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillRunState ResumeMethodDrillRunFromBatch(
		const FHyperTwistTrainingMethodDrillDefinition& DrillDefinition,
		const FHyperTwistTrainingMethodDrillBatch& DrillBatch,
		const FString& RunId
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillRunState StartNextActiveMethodDrillCase();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillRunState RevealActiveMethodDrillCase();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillRunState RepeatActiveMethodDrillCase();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillRunState AdvanceActiveMethodDrillCase();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingMethodDrillRunState FinishActiveMethodDrillRun(bool bAbortRun);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	void ClearActiveMethodDrillRun();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingRunStepResult SubmitTrainingAttempt(
		const FHyperTwistTrainingAttempt& Attempt,
		int32 TimeMs
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool StartActiveLiveTimer();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	bool StartActiveLiveTimerForCurrentDrillCase();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool AdvanceActiveLiveTimerToSolvePhase();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool PauseActiveLiveTimer();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool ResumeActiveLiveTimer();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	void CancelActiveLiveTimer();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool CaptureActiveLiveTimerSplit();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	bool UndoActiveLiveTimerSplit();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingRunStepResult SubmitActiveLiveTimedAttempt(EHyperTwistTrainingAttemptResult Result);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Drill")
	FHyperTwistTrainingDrillTimerCompletionResult SubmitAndAdvanceActiveDrillTimerAttempt(
		EHyperTwistTrainingAttemptResult Result
	);

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|SmartDevice")
	bool HasActiveSmartDeviceSession() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|Training|SmartDevice")
	FHyperTwistTrainingSmartDeviceSessionRecord GetActiveSmartDeviceSessionRecord() const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|SmartDevice")
	FHyperTwistTrainingSmartDeviceSessionRecord StartActiveSmartDeviceSession(
		const FHyperTwistTrainingSmartDeviceIdentity& DeviceIdentity,
		const FString& DeviceSessionId
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|SmartDevice")
	FHyperTwistTrainingSmartDeviceSessionRecord StopActiveSmartDeviceSession(const FString& CloseReason);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|SmartDevice")
	FHyperTwistTrainingSmartDeviceIngestionResult IngestSmartDeviceEventFrame(
		const FHyperTwistTrainingSmartDeviceEventFrame& EventFrame
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	FHyperTwistTrainingSession CompleteActiveRun(bool bAbortSession);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training")
	void ClearActiveRun();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Recognition")
	bool OpenActiveRecognitionSession(FString& OutError);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Recognition")
	FHyperTwistVisionPreviewResult SubmitActiveRecognitionFrame(const FHyperTwistVisionFrameEnvelope& Frame);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Recognition")
	FHyperTwistVisionCommitResult CommitActiveRecognitionObservation(const FHyperTwistVisionCommitRequest& Request);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Recognition")
	FHyperTwistVisionFinalizeResult FinalizeActiveRecognitionSession();

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Recognition")
	bool CloseActiveRecognitionSession(FString& OutError);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Speech")
	bool OpenActiveCompanionSpeechSession(FString& OutError);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Speech")
	FHyperTwistSpeechTranscriptResult SubmitActiveCompanionSpeechUtterance(
		const FHyperTwistSpeechUtteranceEnvelope& Utterance
	);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|Training|Speech")
	bool CloseActiveCompanionSpeechSession(FString& OutError);

	void SetActiveAlgJsSequence(const FHyperTwistAlgorithmSequence& Sequence);
	bool TryGetActiveAlgJsSequence(FHyperTwistAlgorithmSequence& OutSequence) const;

private:
	void RefreshSummary();
	void RefreshMethodDrillSummary();
	void RefreshRepositoryViews();
	void ResetActiveLiveTimerState();
	void RefreshActiveLiveTimerState();
	bool CanUseSmartDeviceBridge() const;
	bool TryResolveSmartDeviceAdapterBoundaryId(
		const FString& RequestedBoundaryId,
		FString& OutBoundaryId
	) const;
	FHyperTwistTrainingSmartDeviceIdentity NormalizeSmartDeviceIdentity(
		const FHyperTwistTrainingSmartDeviceIdentity& DeviceIdentity
	) const;
	FHyperTwistTrainingSmartDeviceEventFrame NormalizeSmartDeviceEventFrame(
		const FHyperTwistTrainingSmartDeviceEventFrame& EventFrame
	) const;
	FHyperTwistTrainingAttempt BuildSmartDeviceAttemptFromEventFrame(
		const FHyperTwistTrainingSmartDeviceEventFrame& EventFrame
	) const;
	void MergeSmartDeviceSplitCapturesIntoLiveTimer(
		const TArray<FHyperTwistTrainingSplitCapture>& SplitCaptures
	);
	FHyperTwistTrainingLiveTimerState BuildActiveLiveTimerStateSnapshot(
		double NowSeconds,
		const FString& NowUtc
	) const;
	FHyperTwistVisionSessionConfig BuildActiveRecognitionSessionConfig() const;
	FHyperTwistSpeechSessionConfig BuildActiveCompanionSpeechSessionConfig() const;
	UObject* ResolveRecognitionVisionClientObject();
	UObject* ResolveCompanionSpeechClientObject();
	void RefreshRecognitionServiceHealth();
	void RefreshCompanionSpeechServiceHealth();
	void AppendRecognitionReplayEvent(
		EHyperTwistReplayEventType EventType,
		const FHyperTwistReplayRecognitionPayload& Payload,
		int32 PreferredTimeMs
	);
	void SynchronizeRecognitionReplayMutation();
	void ResetRecognitionSessionState();
	void ResetCompanionSpeechSessionState();
	TArray<FHyperTwistTrainingSessionTemplate> BuildTrainingSessionTemplatesForUser(
		const FString& UserId,
		const FString& ReferenceUtc
	) const;
	bool TryResolveRepositoryViewContext(
		FString& OutUserId,
		FString& OutDeckId,
		FString& OutReferenceUtc
	) const;
	FHyperTwistTrainingDeck ResolveRepositoryFocusDeck() const;

	UPROPERTY()
	FHyperTwistTrainingRunState ActiveRunState;

	UPROPERTY()
	FHyperTwistTrainingSessionSummary ActiveRunSummary;

	UPROPERTY()
	FHyperTwistTrainingLiveTimerState ActiveLiveTimerState;

	UPROPERTY()
	FHyperTwistTrainingSmartDeviceSessionRecord ActiveSmartDeviceSessionRecord;

	UPROPERTY()
	FString RetainedRepositoryViewUserId;

	UPROPERTY()
	FString RetainedRepositoryViewDeckId;

	UPROPERTY()
	FString RetainedRepositoryViewReferenceUtc;

	UPROPERTY()
	FHyperTwistTrainingImportedGeneratedModeLaunchRequest ActiveImportedGeneratedModeLaunchRequest;

	UPROPERTY()
	FHyperTwistTrainingMethodDrillRunState ActiveMethodDrillRunState;

	UPROPERTY()
	FHyperTwistTrainingMethodDrillSummary ActiveMethodDrillSummary;

	UPROPERTY()
	FHyperTwistTrainingMethodDrillDiagnosticPacket ActiveMethodDrillDiagnosticPacket;

	UPROPERTY()
	FHyperTwistTrainingSessionTemplate ActiveMethodDrillFollowUpTemplate;

	UPROPERTY()
	FHyperTwistTrainingRepositoryState TrainingRepositoryState;

	UPROPERTY()
	FHyperTwistTrainingRepositoryIntegrityReport ActiveRepositoryIntegrityReport;

	UPROPERTY()
	FHyperTwistTrainingRepositoryRoundTripVerification ActiveRepositoryRoundTripVerification;

	UPROPERTY()
	FHyperTwistTrainingDeckHistorySummary ActiveDeckHistorySummary;

	UPROPERTY()
	FHyperTwistTrainingTimingTrendSummary ActiveTimingTrendSummary;

	UPROPERTY()
	FHyperTwistTrainingPersonalBestSummary ActivePersonalBestSummary;

	UPROPERTY()
	FHyperTwistTrainingDeckScopedStats ActiveDeckScopedStats;

	UPROPERTY()
	FHyperTwistTrainingCaseScopedStats ActiveCurrentCaseScopedStats;

	UPROPERTY()
	FHyperTwistTrainingLearnerDeckStateSummary ActiveLearnerDeckStateSummary;

	UPROPERTY()
	FHyperTwistTrainingLearnerProfile ActiveLearnerProfile;

	UPROPERTY()
	FHyperTwistTrainingPublicationBundle ActivePublicationBundle;

	UPROPERTY()
	FHyperTwistTrainingSocialChallengeBundle ActiveSocialChallengeBundle;

	UPROPERTY()
	FHyperTwistTrainingCoachMemorySnapshot ActiveCoachMemorySnapshot;

	UPROPERTY()
	FHyperTwistReplayReviewAnalytics ActiveReplayReviewAnalytics;

	UPROPERTY()
	FHyperTwistRecognitionReplaySummary ActiveRecognitionReplaySummary;

	UPROPERTY()
	FHyperTwistTrainingRecognitionSessionState ActiveRecognitionSessionState;

	UPROPERTY()
	FHyperTwistTrainingCompanionSpeechSessionState ActiveCompanionSpeechSessionState;

	UPROPERTY()
	TArray<FHyperTwistCoachSignal> ActiveCoachSignals;

	UPROPERTY()
	FHyperTwistCoachBrief ActiveCoachBrief;

	UPROPERTY()
	FHyperTwistTrainingCoachActionPlan ActiveCoachActionPlan;

	UPROPERTY()
	FHyperTwistTrainingCoachActionPlanSummary ActiveCoachActionPlanSummary;

	UPROPERTY()
	FHyperTwistTrainingCoachActionPlanOutcomeSummary ActiveCoachActionPlanOutcomeSummary;

	UPROPERTY()
	FHyperTwistTrainingCoachActionSequenceSummary ActiveCoachActionSequenceSummary;

	UPROPERTY()
	FHyperTwistTrainingCoachActionClosureSummary ActiveCoachActionClosureSummary;

	UPROPERTY()
	FHyperTwistTrainingCoachClosureMemorySummary ActiveCoachClosureMemorySummary;

	UPROPERTY()
	FHyperTwistTrainingCoachRecommendationHistorySummary ActiveCoachRecommendationHistorySummary;

	UPROPERTY()
	FHyperTwistTrainingCoachCarryForwardPolicySummary ActiveCoachCarryForwardPolicySummary;

	UPROPERTY()
	FHyperTwistTrainingCoachArchivePolicySummary ActiveCoachArchivePolicySummary;

	UPROPERTY()
	FHyperTwistCoachBrief ActiveCoachFollowUpBrief;

	UPROPERTY()
	FHyperTwistCoachSessionQueueSummary ActiveCoachSessionQueueSummary;

	UPROPERTY()
	FHyperTwistTrainingCoachSessionQueueState ActiveCoachSessionQueueState;

	UPROPERTY()
	FHyperTwistCoachScheduleHorizonSummary ActiveCoachScheduleHorizonSummary;

	UPROPERTY()
	FHyperTwistCoachSchedulePolicySummary ActiveCoachSchedulePolicySummary;

	UPROPERTY()
	FHyperTwistTrainingCoachSessionQueueExecutionSummary ActiveCoachSessionQueueExecutionSummary;

	UPROPERTY()
	TArray<FHyperTwistTrainingMethodSegment> ActiveLearnerMethodSegments;

	UPROPERTY()
	TArray<FHyperTwistTrainingLearnerPreset> ActiveLearnerPresets;

	UPROPERTY()
	TArray<FHyperTwistTrainingSessionTemplate> ActiveTrainingSessionTemplates;

	UPROPERTY()
	TArray<FHyperTwistTrainingCaseRecommendation> ActiveCaseRecommendations;

	UPROPERTY()
	FHyperTwistTrainingReviewPlanState ActiveReviewPlanState;

	UPROPERTY()
	FHyperTwistTrainingReviewFlowStatus ActiveReviewFlowStatus;

	UPROPERTY()
	FHyperTwistTrainingReviewProgramSummary ActiveReviewProgramSummary;

	UPROPERTY(Config)
	FString RecognitionClientKind = TEXT("http-sidecar");

	UPROPERTY()
	TObjectPtr<UObject> ActiveRecognitionVisionClientObject;

	UPROPERTY(Config)
	FString CompanionSpeechClientKind = TEXT("http-sidecar");

	UPROPERTY()
	TObjectPtr<UObject> ActiveCompanionSpeechClientObject;

	UPROPERTY()
	bool bHasActiveRun = false;

	UPROPERTY()
	bool bHasActiveMethodDrillRun = false;

	UPROPERTY()
	bool bHasActiveSmartDeviceSession = false;

	double ActiveLiveTimerStartedAtSeconds = 0.0;
	double ActiveLiveTimerSolveStartedAtSeconds = 0.0;
	double ActiveLiveTimerPausedAtSeconds = 0.0;
	int32 ActiveLiveTimerStoredInspectionElapsedMs = 0;
	int32 ActiveLiveTimerStoredSolveElapsedMs = 0;

	bool bHasActiveAlgJsSequence = false;
	FHyperTwistAlgorithmSequence ActiveAlgJsSequence;
};
