#include "ZeusCharacter.h"

#include "Animation/AnimInstance.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "EnhancedInputComponent.h"
#include "Engine/GameInstance.h"
#include "Engine/SkeletalMesh.h"
#include "Engine/World.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
#include "InputAction.h"
#include "InputActionValue.h"
#include "GameFramework/PlayerState.h"
#include "Subsystems/WorldSubsystem.h"
#include "UObject/ConstructorHelpers.h"
#include "ZMMO.h"
#include "GameplayTagContainer.h"
#include "GameplayTagsManager.h"
#include "ZeusGASComponent.h"
#include "ZeusAOIComponent.h"
#include "ZeusPlayerState.h"
#include "Blueprint/UserWidget.h"
#include "GameFramework/PlayerController.h"
#include "ZeusWorldSubsystem.h"
#include "ZeusNetworkSubsystem.h"
#include "UI/FrontEnd/UIFrontEndFlowSubsystem.h"

DEFINE_LOG_CATEGORY(LogZeusPlayer);

AZeusCharacter::AZeusCharacter()
{
	PrimaryActorTick.bCanEverTick = true;
	PrimaryActorTick.bStartWithTickEnabled = true;

	GetCapsuleComponent()->InitCapsuleSize(42.0f, 96.0f);

	bUseControllerRotationPitch = false;
	bUseControllerRotationYaw = false;
	bUseControllerRotationRoll = false;

	UCharacterMovementComponent* CMC = GetCharacterMovement();
	CMC->bOrientRotationToMovement = true;
	CMC->RotationRate = FRotator(0.0f, 500.0f, 0.0f);
	CMC->JumpZVelocity = 500.0f;
	CMC->AirControl = 0.35f;
	CMC->MaxWalkSpeed = 500.0f;
	CMC->MinAnalogWalkSpeed = 20.0f;
	CMC->BrakingDecelerationWalking = 2000.0f;
	CMC->BrakingDecelerationFalling = 1500.0f;

	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	CameraBoom->SetupAttachment(RootComponent);
	CameraBoom->TargetArmLength = 400.0f;
	CameraBoom->bUsePawnControlRotation = true;

	FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
	FollowCamera->bUsePawnControlRotation = false;

	// AOI do cliente (+ overlays de debug). Implementa IZeusAOIDebugTarget; o
	// Zeus Admin Tools acha este componente no pawn e dirige os overlays.
	AOIComponent = CreateDefaultSubobject<UZeusAOIComponent>(TEXT("ZeusAOIComponent"));

	// AdminPanelClass NAO e' resolvido aqui via FClassFinder: na criacao do CDO
	// (load do modulo) o asset registry pode nao estar pronto e o static cacheia
	// null pra sempre. Resolvido lazy em ToggleAdminPanel (LoadClass em runtime).

	// AttributeComponent migrou pro PlayerState (AZeusPlayerState + Component
	// Registry config-driven). Pawn fica leve — so' movement/input/camera.

	// Defaults visuais (mesh + AnimBP). Permitem ao motor spawnar AZeusCharacter
	// directamente como DefaultPawnClass do AZeusGameMode sem exigir um BP filho.
	// Um BP filho continua opcional para trocar mesh/AnimBP por mapa.
	if (USkeletalMeshComponent* MeshComponent = GetMesh())
	{
		static ConstructorHelpers::FObjectFinder<USkeletalMesh> QuinnMesh(
			TEXT("/Game/Characters/Mannequins/Meshes/SKM_Quinn_Simple.SKM_Quinn_Simple"));
		static ConstructorHelpers::FClassFinder<UAnimInstance> QuinnAnimBp(
			TEXT("/Game/Characters/Mannequins/Anims/Unarmed/ABP_Unarmed"));

		if (QuinnMesh.Succeeded())
		{
			MeshComponent->SetSkeletalMesh(QuinnMesh.Object);
			MeshComponent->SetRelativeLocation(FVector(0.0f, 0.0f, -90.0f));
			MeshComponent->SetRelativeRotation(FRotator(0.0f, -90.0f, 0.0f));
		}
		else
		{
			UE_LOG(LogZeusPlayer, Warning, TEXT("Default mesh SKM_Quinn_Simple not found for AZeusCharacter."));
		}

		if (QuinnAnimBp.Succeeded())
		{
			MeshComponent->SetAnimationMode(EAnimationMode::AnimationBlueprint);
			MeshComponent->SetAnimInstanceClass(QuinnAnimBp.Class);
		}
		else
		{
			UE_LOG(LogZeusPlayer, Warning, TEXT("Default anim blueprint ABP_Unarmed not found for AZeusCharacter."));
		}
	}

	// Input Actions defaults (Enhanced Input) — IMCs sao adicionados pelo
	// AZeusPlayerController. As IAs aqui resolvem o asset por path; um BP filho
	// pode sobrescrever caso queira inputs custom.
	static ConstructorHelpers::FObjectFinder<UInputAction> MoveActionAsset(
		TEXT("/Game/Input/Actions/IA_Move.IA_Move"));
	static ConstructorHelpers::FObjectFinder<UInputAction> LookActionAsset(
		TEXT("/Game/Input/Actions/IA_Look.IA_Look"));
	static ConstructorHelpers::FObjectFinder<UInputAction> MouseLookActionAsset(
		TEXT("/Game/Input/Actions/IA_MouseLook.IA_MouseLook"));
	static ConstructorHelpers::FObjectFinder<UInputAction> JumpActionAsset(
		TEXT("/Game/Input/Actions/IA_Jump.IA_Jump"));
	static ConstructorHelpers::FObjectFinder<UInputAction> DashActionAsset(
		TEXT("/Game/Input/Actions/IA_Dash.IA_Dash"));

	if (MoveActionAsset.Succeeded()) { MoveAction = MoveActionAsset.Object; }
	if (LookActionAsset.Succeeded()) { LookAction = LookActionAsset.Object; }
	if (MouseLookActionAsset.Succeeded()) { MouseLookAction = MouseLookActionAsset.Object; }
	if (JumpActionAsset.Succeeded()) { JumpAction = JumpActionAsset.Object; }
	if (DashActionAsset.Succeeded()) { DashAction = DashActionAsset.Object; }
}

void AZeusCharacter::BeginPlay()
{
	Super::BeginPlay();
	ResolveZeusNetworkSubsystem();
	BindZeusSpawnDelegate();
	TryRegisterLocalEntityFromCachedSpawn();

	// Fase 4: reposiciona o pawn na pos salva no DB (vinda no S_CHAR_SELECT_OK
	// e memorizada no Flow). Substitui o PlayerStart default do level. Se nao
	// ha pose pendente (primeiro boot / debug PIE direto no level), mantem o
	// PlayerStart.
	if (const UGameInstance* GI = GetGameInstance())
	{
		if (UUIFrontEndFlowSubsystem* Flow = GI->GetSubsystem<UUIFrontEndFlowSubsystem>())
		{
			FVector PosCm = FVector::ZeroVector;
			float YawDeg = 0.0f;
			if (Flow->ConsumePendingSpawnPose(PosCm, YawDeg))
			{
				const FRotator NewRot(0.0f, YawDeg, 0.0f);
				SetActorLocationAndRotation(PosCm, NewRot, /*bSweep=*/false, nullptr, ETeleportType::TeleportPhysics);
				if (AController* C = GetController())
				{
					C->SetControlRotation(NewRot);
				}
				UE_LOG(LogZeusPlayer, Log,
					TEXT("AZeusCharacter: pawn reposicionado pra pos do DB (%s) yaw=%.1f"),
					*PosCm.ToString(), YawDeg);
			}
		}
	}
}

void AZeusCharacter::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	UnbindZeusSpawnDelegate();
	ZeusNetwork = nullptr;
	Super::EndPlay(EndPlayReason);
}

void AZeusCharacter::Tick(const float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);
	FlushInputAxisToServer(DeltaSeconds);
}

void AZeusCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent))
	{
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &AZeusCharacter::OnJumpPressed);
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &AZeusCharacter::OnJumpReleased);

		EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AZeusCharacter::Move);
		EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Completed, this, &AZeusCharacter::MoveCompleted);

		EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &AZeusCharacter::Look);
		EnhancedInputComponent->BindAction(MouseLookAction, ETriggerEvent::Triggered, this, &AZeusCharacter::Look);

		if (DashAction)
		{
			EnhancedInputComponent->BindAction(DashAction, ETriggerEvent::Triggered, this, &AZeusCharacter::OnDashTriggered);
		}
	}
	else
	{
		UE_LOG(LogZeusPlayer, Error, TEXT("'%s' Failed to find an Enhanced Input component."), *GetNameSafe(this));
	}

	// Debug: F8 abre/fecha o Zeus Admin Tools (AIO Debug). Legacy BindKey
	// convive com Enhanced Input. Trocar a tecla aqui se conflitar.
	PlayerInputComponent->BindKey(EKeys::F8, IE_Pressed, this, &AZeusCharacter::ToggleAdminPanel);
}

void AZeusCharacter::ToggleAdminPanel()
{
	APlayerController* PC = Cast<APlayerController>(GetController());
	if (!PC)
	{
		return;
	}

	// Aberto -> fecha + devolve input pro jogo.
	if (AdminPanelInstance && AdminPanelInstance->IsInViewport())
	{
		AdminPanelInstance->RemoveFromParent();
		PC->SetInputMode(FInputModeGameOnly());
		PC->bShowMouseCursor = false;
		return;
	}

	// Lazy load (runtime): no F8 o asset ja' esta carregavel; evita o pitfall do
	// FClassFinder na criacao do CDO. Path do generated class (_C).
	// A UI vive no CONTENT DO PLUGIN (mount /ZeusAdminTools/), nao no /Game do projeto:
	// o plugin ZeusAdminTools e' autocontido (C++ + UI no repo Server).
	if (!AdminPanelClass)
	{
		AdminPanelClass = LoadClass<UUserWidget>(nullptr,
			TEXT("/ZeusAdminTools/UI/WBP_AdminToolsAIO.WBP_AdminToolsAIO_C"));
	}
	if (!AdminPanelClass)
	{
		UE_LOG(LogZeusPlayer, Warning, TEXT("[AdminPanel] AdminPanelClass nao resolvido (WBP_AdminToolsAIO ausente?)."));
		return;
	}

	if (!AdminPanelInstance)
	{
		AdminPanelInstance = CreateWidget<UUserWidget>(PC, AdminPanelClass);
	}
	if (AdminPanelInstance)
	{
		AdminPanelInstance->AddToViewport(1000);

		// UIOnly: trava a acao do jogo enquanto o menu esta aberto e garante que
		// os cliques vao 100% pra UI (no GameAndUI o viewport rouba o clique).
		// O mundo continua renderizando + os overlays de debug seguem desenhando.
		FInputModeUIOnly Mode;
		Mode.SetWidgetToFocus(AdminPanelInstance->TakeWidget());
		Mode.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
		PC->SetInputMode(Mode);
		PC->bShowMouseCursor = true;
	}
}

void AZeusCharacter::Move(const FInputActionValue& Value)
{
	const FVector2D MovementVector = Value.Get<FVector2D>();
	DoMove(MovementVector.X, MovementVector.Y);
}

void AZeusCharacter::MoveCompleted(const FInputActionValue& Value)
{
	(void)Value;
	DoMove(0.0f, 0.0f);
}

void AZeusCharacter::Look(const FInputActionValue& Value)
{
	const FVector2D LookAxisVector = Value.Get<FVector2D>();
	DoLook(LookAxisVector.X, LookAxisVector.Y);
}

void AZeusCharacter::OnJumpPressed()
{
	bPendingJumpPressed = true;
	DoJumpStart();
}

void AZeusCharacter::OnJumpReleased()
{
	bPendingJumpReleased = true;
	DoJumpEnd();
}

void AZeusCharacter::OnDashTriggered()
{
	// Resolve UZeusGASComponent do PlayerState. Component vive la' via
	// AZeusPlayerState Component Registry (config-driven). Sem el, ASC ausente
	// e RequestActivateAbilityByTag falha — log warning.
	const APlayerState* PS = GetPlayerState();
	if (!PS)
	{
		UE_LOG(LogZeusPlayer, Warning, TEXT("OnDashTriggered: PlayerState nullptr — ignorado"));
		return;
	}
	UZeusGASComponent* Comp = PS->FindComponentByClass<UZeusGASComponent>();
	if (!Comp)
	{
		UE_LOG(LogZeusPlayer, Warning, TEXT("OnDashTriggered: UZeusGASComponent nao achado"));
		return;
	}

	// Tag canonica casa com row do DT + JSON do server. Hash FNV calculado
	// dentro do componente (RequestActivateAbilityByTag).
	const FGameplayTag DashTag = UGameplayTagsManager::Get().RequestGameplayTag(
		FName(TEXT("Zeus.Ability.Movement.Dash")));
	if (!DashTag.IsValid())
	{
		UE_LOG(LogZeusPlayer, Warning,
			TEXT("OnDashTriggered: tag Zeus.Ability.Movement.Dash nao registrada no cliente"));
		return;
	}

	const bool bSent = Comp->RequestActivateAbilityByTag(DashTag);
	UE_LOG(LogZeusPlayer, Log, TEXT("OnDashTriggered -> RequestActivateAbilityByTag = %s"),
		bSent ? TEXT("OK (C_ABILITY_TRY_ACTIVATE enviado)") : TEXT("FALHOU"));
}

void AZeusCharacter::DoMove(const float Right, const float Forward)
{
	PendingMoveRight = FMath::Clamp(Right, -1.0f, 1.0f);
	PendingMoveForward = FMath::Clamp(Forward, -1.0f, 1.0f);

	if (GetController() != nullptr)
	{
		const FRotator Rotation = GetController()->GetControlRotation();
		const FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
		const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
		const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
		AddMovementInput(ForwardDirection, Forward);
		AddMovementInput(RightDirection, Right);
	}
}

void AZeusCharacter::DoLook(const float Yaw, const float Pitch)
{
	if (GetController() != nullptr)
	{
		AddControllerYawInput(Yaw);
		AddControllerPitchInput(Pitch);
	}
}

void AZeusCharacter::DoJumpStart()
{
	Jump();
}

void AZeusCharacter::DoJumpEnd()
{
	StopJumping();
}

void AZeusCharacter::ApplyEntitySnapshot(const FZeusEntitySnapshot& /*Snapshot*/)
{
	// Cliente local solto: o servidor nao reconcilia posicao em V0 (ADR 0038).
	// Quando colisao de objetos chegar, podemos ligar uma reconciliacao opcional
	// horizontal aqui (XY only), preservando o Z do CMC local.
}

void AZeusCharacter::SetEntityRelevant(bool /*bRelevant*/)
{
	// Nunca despawnamos o jogador local via AOI.
}

void AZeusCharacter::ResolveZeusNetworkSubsystem()
{
	if (ZeusNetwork)
	{
		return;
	}

	UGameInstance* GI = GetGameInstance();
	if (!GI)
	{
		return;
	}

	ZeusNetwork = GI->GetSubsystem<UZeusNetworkSubsystem>();
}

void AZeusCharacter::BindZeusSpawnDelegate()
{
	if (bSpawnDelegateBound || !IsLocallyControlled())
	{
		return;
	}
	if (!ZeusNetwork)
	{
		return;
	}

	ZeusNetwork->OnPlayerSpawned.AddDynamic(this, &AZeusCharacter::HandleZeusPlayerSpawned);
	ZeusNetwork->OnCharInfoReceived.AddDynamic(this, &AZeusCharacter::HandleZeusCharInfo);
	bSpawnDelegateBound = true;
}

void AZeusCharacter::UnbindZeusSpawnDelegate()
{
	if (!bSpawnDelegateBound || !ZeusNetwork)
	{
		bSpawnDelegateBound = false;
		return;
	}
	ZeusNetwork->OnPlayerSpawned.RemoveDynamic(this, &AZeusCharacter::HandleZeusPlayerSpawned);
	ZeusNetwork->OnCharInfoReceived.RemoveDynamic(this, &AZeusCharacter::HandleZeusCharInfo);
	bSpawnDelegateBound = false;
}

void AZeusCharacter::TryRegisterLocalEntityFromCachedSpawn()
{
	if (!ZeusNetwork)
	{
		return;
	}

	int64 CachedEntityId = 0;
	FVector CachedPosCm = FVector::ZeroVector;
	float CachedYawDeg = 0.0f;
	int64 CachedServerTimeMs = 0;
	if (ZeusNetwork->TryGetLastLocalSpawn(CachedEntityId, CachedPosCm, CachedYawDeg, CachedServerTimeMs))
	{
		HandleLocalSpawnReady(CachedEntityId, CachedPosCm, CachedYawDeg, CachedServerTimeMs);
	}

	// Race fix: o S_CHAR_INFO chega ANTES de S_SPAWN_PLAYER mas o pawn pode
	// nao existir ainda (OpenLevel em andamento). Aplica o ultimo cacheado.
	TryApplyCachedCharInfo();
}

void AZeusCharacter::HandleZeusPlayerSpawned(const int64 InEntityId, const bool bIsLocal,
	const FVector PosCm, const float YawDeg, const int64 ServerTimeMs)
{
	if (!bIsLocal)
	{
		// Remote spawns sao tratados pelo `UZeusWorldSubsystem`; aqui so reagimos
		// ao spawn do pawn possessivel local (S_SPAWN_PLAYER com bIsLocal=true).
		return;
	}
	HandleLocalSpawnReady(InEntityId, PosCm, YawDeg, ServerTimeMs);
}

void AZeusCharacter::HandleLocalSpawnReady(const int64 InEntityId, const FVector PosCm,
	const float YawDeg, const int64 ServerTimeMs)
{
	if (InEntityId == 0)
	{
		return;
	}

	// PR-HANDOFF-007 — InEntityId é int64 (era int32)
	if (ZeusEntityId == InEntityId)
	{
		return;
	}

	ZeusEntityId = InEntityId;

	UE_LOG(LogZeusPlayer, Log,
		TEXT("AZeusCharacter: local spawn captured EntityId=%lld pos=(%s) yaw=%.1f t=%lld"),
		InEntityId, *PosCm.ToString(), YawDeg, ServerTimeMs);

	if (UWorld* World = GetWorld())
	{
		if (UZeusWorldSubsystem* WorldSubsystem = World->GetSubsystem<UZeusWorldSubsystem>())
		{
			WorldSubsystem->RegisterLocalEntity(InEntityId, this);
		}
	}

	// Identidade publica (EntityId) + seed no GAS Component. CharName/Guild
	// vem separado via S_CHAR_INFO -> HandleZeusCharInfo. UZeusGASComponent
	// vive como subobject do PlayerState (Component Registry) e recebe seed
	// pra que o UZeusGASNetworkHandler consiga rotear o snapshot por
	// EntityId desde o primeiro pacote.
	if (APlayerState* PS = GetPlayerState())
	{
		if (AZeusPlayerState* ZeusPs = Cast<AZeusPlayerState>(PS))
		{
			ZeusPs->SetPublicIdentity(InEntityId, /*CharId*/ FString(),
				/*BaseLevel*/ 1, /*ClassId*/ 0);
		}
		if (UZeusGASComponent* GASComp = PS->FindComponentByClass<UZeusGASComponent>())
		{
			GASComp->SeedEntityId(InEntityId);
		}
	}

	// Aplica char info cacheada (caso S_CHAR_INFO tenha chegado antes do PS existir).
	TryApplyCachedCharInfo();

	// UI in-game e' responsabilidade do AZeusHUD (GameMode.HUDClass), nao do
	// Pawn. AHUD::BeginPlay chama UUIInGameFlowSubsystem::StartInGame, que
	// pushea WBP_HUD em UI.Layer.Game. UZeusHudWidget::NativeOnActivated auto-binda
	// no UZeusGASComponent via PlayerState.
}

void AZeusCharacter::HandleZeusCharInfo(const int64 InEntityId, const FString& CharName, const FString& GuildName)
{
	UE_LOG(LogZeusPlayer, Log,
		TEXT("AZeusCharacter: S_CHAR_INFO recebido EntityId=%lld name=%s guild=%s"),
		InEntityId, *CharName, *GuildName);

	// 2026-06-03 fix: server envia S_CHAR_INFO de TODOS os players (proprio +
	// catch-up de proxies pre-existentes). Sem este filtro, o nome do ultimo
	// proxy recebido sobrescrevia o do pawn local (ex: player Mateuus loga e
	// ve "Olatudook" em si mesmo porque o catch-up do Olatudook chegou depois
	// do S_CHAR_INFO proprio). Proxies remotos sao roteados pelo registry no
	// UZeusWorldSubsystem (futuro nameplate por EntityId).
	if (ZeusEntityId != 0 && InEntityId != ZeusEntityId)
	{
		return;
	}

	APlayerState* PS = GetPlayerState();
	if (!PS)
	{
		// Race fix: pawn pode nao ter PlayerState ainda. Subsystem ja cacheou
		// (LastLocalCharInfoCache); TryApplyCachedCharInfo aplica em
		// HandleLocalSpawnReady ou em outra oportunidade.
		return;
	}
	if (AZeusPlayerState* ZeusPs = Cast<AZeusPlayerState>(PS))
	{
		ZeusPs->SetCharInfo(CharName, GuildName);
	}
}

void AZeusCharacter::TryApplyCachedCharInfo()
{
	if (!ZeusNetwork)
	{
		return;
	}
	int64 CachedEntityId = 0;
	FString CachedCharName;
	FString CachedGuildName;
	if (!ZeusNetwork->TryGetLastLocalCharInfo(CachedEntityId, CachedCharName, CachedGuildName))
	{
		return;
	}
	if (APlayerState* PS = GetPlayerState())
	{
		if (AZeusPlayerState* ZeusPs = Cast<AZeusPlayerState>(PS))
		{
			ZeusPs->SetCharInfo(CachedCharName, CachedGuildName);
		}
	}
}

void AZeusCharacter::FlushInputAxisToServer(const float DeltaSeconds)
{
	ResolveZeusNetworkSubsystem();
	if (!ZeusNetwork || !ZeusNetwork->IsConnected())
	{
		return;
	}

	const float SendInterval = 1.0f / FMath::Max(1, InputSendRateHz);
	SendAccumulatorSec += DeltaSeconds;
	TimeSinceLastSendSec += DeltaSeconds;

	const bool bMovingInput = !FMath::IsNearlyZero(PendingMoveForward) || !FMath::IsNearlyZero(PendingMoveRight);
	const bool bJumpEvent = bPendingJumpPressed || bPendingJumpReleased;
	const bool bRateReached = SendAccumulatorSec >= SendInterval;
	const bool bHeartbeatReached = TimeSinceLastSendSec >= HeartbeatIntervalSec;

	// IsFalling é estado contínuo do CMC (espelhado em PlayerStatePayload::grounded
	// para alimentar o MovementMode dos proxies). Forçar envio em transições
	// walking↔falling garante que a animação de queda/pulo dispare nos outros
	// clientes mesmo quando estamos em rate limit / sem input de movimento.
	const UCharacterMovementComponent* CMCForFalling = GetCharacterMovement();
	const bool bIsFalling = CMCForFalling != nullptr && CMCForFalling->IsFalling();
	const bool bFallingChanged = (bIsFalling != bPreviousFalling);

	const bool bShouldSend = bJumpEvent || bFallingChanged || (bMovingInput && bRateReached) || (!bMovingInput && bHeartbeatReached);
	if (!bShouldSend)
	{
		return;
	}

	++InputSequence;
	const int32 ClientTimeMs = static_cast<int32>(FPlatformTime::Seconds() * 1000.0);
	// ADR 0040: yaw da camara/controller para que o servidor consiga rotar o
	// input (referencial-camara) para velocidade mundial. Fallback para o yaw
	// do actor se nao houver controller (e.g. tela de loading).
	const float ViewYawDeg = (GetController()
		? static_cast<float>(GetController()->GetControlRotation().Yaw)
		: static_cast<float>(GetActorRotation().Yaw));
	// ADR 0041: cliente autoritativo da posicao XYZ + velocidade XY. O CMC ja
	// integrou o input local antes deste flush, portanto `GetActorLocation` e
	// `GetVelocity` reflectem o estado real que o servidor deve replicar para
	// os outros clientes (sujeito ao clamp anti-cheat).
	const FVector PosCm = GetActorLocation();
	const FVector Vel = GetVelocity();
	ZeusNetwork->SendInputAxis(
		PendingMoveForward,
		PendingMoveRight,
		bPendingJumpPressed,
		bPendingJumpReleased,
		InputSequence,
		ClientTimeMs,
		ViewYawDeg,
		PosCm,
		FVector2D(Vel.X, Vel.Y),
		bIsFalling,
		static_cast<float>(Vel.Z));

	SendAccumulatorSec = 0.0f;
	TimeSinceLastSendSec = 0.0f;
	bPendingJumpPressed = false;
	bPendingJumpReleased = false;
	bPreviousFalling = bIsFalling;
}
