// Copyright Zeus Server Engine. All rights reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ZeusV1Protocol.h" // ZeusV1::FDebugFrontierInfo (plugin ZeusNetwork)
#include "ZeusFrontierOverlayActor.generated.h"

class UProceduralMeshComponent;
class UMaterialInterface;

/**
 *  AZeusFrontierOverlayActor (cliente / debug)
 *
 *  Desenha no MUNDO as "paredes" de TODAS as fronteiras do grid de server-meshing
 *  (todas as arestas compartilhadas entre quaisquer duas cells, com dedup), usando
 *  a material de portal MI_VFX_Portal_GridBoundary_01. As cells vem do
 *  DEBUG_FRONTIER_INFO (6163), que reflete a topologia DINAMICA (split/merge).
 *
 *  Label: no CENTRO de cada cell, um texto via DrawDebugString (billboard de tela,
 *  visivel de qualquer lado, nao gira). Mostra a notacao da cell:
 *    raiz   -> "Cell 0", "Cell 1", ...
 *    split  -> "Cell 0,0", "Cell 0,1", ... (pai,quadrante) -- reflete a quadtree.
 *  + o nome do servidor dono. NAO e' replicado: visual no cliente local.
 */
UCLASS()
class ZMMO_API AZeusFrontierOverlayActor : public AActor
{
	GENERATED_BODY()

public:
	AZeusFrontierOverlayActor();

	virtual void Tick(float DeltaSeconds) override;

	/** (Re)constroi as paredes do grid + guarda as cells pros labels.
	 *  AnchorZCm ancora a altura das paredes/labels (tipicamente o Z do player). */
	void RebuildFromFrontier(const ZeusV1::FDebugFrontierInfo& Info, float AnchorZCm);

	/** Limpa a geometria (overlay desligado). */
	void ClearOverlay();

	// === Parametros das paredes ===
	UPROPERTY(EditAnywhere, Category = "Zeus|Frontier") float WallHeightCm  = 12000.0f; // 120m
	UPROPERTY(EditAnywhere, Category = "Zeus|Frontier") float EdgeEpsilonCm = 4.0f;     // tolerancia "compartilha aresta"

	// === Label "Cell X,Y" no centro de cada cell (DrawDebugString) ===
	UPROPERTY(EditAnywhere, Category = "Zeus|Frontier|Label") float LabelFontScale = 1.4f; // tamanho na tela (screen-space)
	UPROPERTY(EditAnywhere, Category = "Zeus|Frontier|Label") float LabelHeightCm  = 250.0f; // altura do label acima do anchor

private:
	UPROPERTY() UProceduralMeshComponent* WallMesh = nullptr;
	UPROPERTY() UMaterialInterface* WallMaterial = nullptr;

	/// Snapshot leve das cells (own + neighbors) pros labels.
	struct FCellView
	{
		uint32  CellId = 0;
		float   MinX = 0.f, MinY = 0.f, MaxX = 0.f, MaxY = 0.f;
		FString Owner;          // "Server1" (ou vazio)
		bool    bDead = false;
	};
	TArray<FCellView> Cells_;
	float AnchorZ_ = 0.0f;
	bool  bHasOverlay_ = false;

	/// Desenha "Cell X,Y\n<server>" no centro de cada cell (chamado no Tick).
	void DrawCellLabels();
};
