MainHud/DiceDuelEventRunner.cs
using Keyboard_Warriors_.Models;
using Keyboard_Warriors_.Systems;
using Sandbox;
using System;

namespace Keyboard_Warriors_.Managers;

public static class DiceDuelEventRunner
{
	private static Player cachedPlayer;
	private static Player cachedAI;
	private static int p1;
	private static int p2;

	// =========================================================================
	// 🌐 MULTIPLAYER SIMULTANEOUS HANDSHAKE REGISTERS
	// =========================================================================
	private static int p1FinalTotal = 0;
	private static int p2FinalTotal = 0;
	private static bool isDuelResolved = false;
	// =========================================================================

	private static int RollDie() => Random.Shared.Next( 1, 7 );

	private static GameManager GetManager()
	{
		return Sandbox.Game.ActiveScene?.Get<GameManager>();
	}

	public static void StartEvent( Player player, Player ai )
	{
		var manager = GetManager();
		if ( manager == null )
		{
			Log.Error( "[DICE DUEL CRITICAL] Failed to resolve GameManager from active scene tree setup!" );
			return;
		}

		bool isMultiplayer = manager.CurrentMode == GameManager.PlayMode.Multiplayer;

		cachedPlayer = player;
		cachedAI = ai;

		// Reset multiplayer registers right on initialization handshake
		p1FinalTotal = 0;
		p2FinalTotal = 0;
		isDuelResolved = false;

		// =========================================================================
		// 👥 MULTIPLAYER SYMMETRICAL POPUP PATHWAYS
		// =========================================================================
		if ( isMultiplayer )
		{
			GameManager.AddLog( "\n=== Live Symmetrical High Dice Duel Begins! ===" );
			GameManager.AddLog( "⚠️ BOTH TERMINALS AWAITING RE-ROLL CHOICE SEQUENCES..." );

			// --- HOST / PLAYER 1 PROMPT LOGIC ---
			int hostD1 = RollDie();
			int hostD2 = RollDie();

			var hostDiceEvent = new ChoiceEvent
			{
				Title = "🎲 HIGH DICE TERMINAL DUEL",
				PromptText = $"Opening Data Array: [{hostD1}] [{hostD2}] (Current Total: {hostD1 + hostD2}). Opponent is selecting side-by-side. Will you lock this score or execute a re-roll sub-routine?",
			};

			Action<int> submitHostTotal = ( finalTotal ) =>
			{
				p1FinalTotal = finalTotal;
				GameManager.AddLog( $"📡 [NET] {cachedPlayer.name} locked their finalized dice total! Awaiting opponent..." );
				CheckMultiplayerResolution();
			};

			hostDiceEvent.Options.Add( new EventOption { ButtonText = "🔒 KEEP CURRENT ROLL", ActionToExecute = () => submitHostTotal( hostD1 + hostD2 ) } );
			hostDiceEvent.Options.Add( new EventOption { ButtonText = "🎲 RE-ROLL DIE 1 ONLY", ActionToExecute = () => submitHostTotal( RollDie() + hostD2 ) } );
			hostDiceEvent.Options.Add( new EventOption { ButtonText = "🎲 RE-ROLL DIE 2 ONLY", ActionToExecute = () => submitHostTotal( hostD1 + RollDie() ) } );
			hostDiceEvent.Options.Add( new EventOption { ButtonText = "💥 RE-ROLL BOTH DICE", ActionToExecute = () => submitHostTotal( RollDie() + RollDie() ) } );

			// --- CLIENT / PLAYER 2 PROMPT LOGIC ---
			int clientD1 = RollDie();
			int clientD2 = RollDie();

			var clientDiceEvent = new ChoiceEvent
			{
				Title = "🎲 HIGH DICE TERMINAL DUEL",
				PromptText = $"Opening Data Array: [{clientD1}] [{clientD2}] (Current Total: {clientD1 + clientD2}). Opponent is selecting side-by-side. Will you lock this score or execute a re-roll sub-routine?",
			};

			Action<int> submitClientTotal = ( finalTotal ) =>
			{
				p2FinalTotal = finalTotal;
				GameManager.AddLog( $"📡 [NET] {cachedAI.name} locked their finalized dice total! Awaiting opponent..." );
				CheckMultiplayerResolution();
			};

			clientDiceEvent.Options.Add( new EventOption { ButtonText = "🔒 KEEP CURRENT ROLL", ActionToExecute = () => submitClientTotal( clientD1 + clientD2 ) } );
			clientDiceEvent.Options.Add( new EventOption { ButtonText = "🎲 RE-ROLL DIE 1 ONLY", ActionToExecute = () => submitClientTotal( RollDie() + clientD2 ) } );
			clientDiceEvent.Options.Add( new EventOption { ButtonText = "🎲 RE-ROLL DIE 2 ONLY", ActionToExecute = () => submitClientTotal( clientD1 + RollDie() ) } );
			clientDiceEvent.Options.Add( new EventOption { ButtonText = "💥 RE-ROLL BOTH DICE", ActionToExecute = () => submitClientTotal( RollDie() + RollDie() ) } );

			// Trigger the respective screen layout popups cleanly based on machine identity tokens
			if ( Networking.IsHost ) manager.TriggerChoicePopup( hostDiceEvent );
			else manager.TriggerChoicePopup( clientDiceEvent );
		}
		else
		{
			// 🤖 SINGLE PLAYER MODE: Your exact original un-edited career loops
			GameManager.AddLog( "\n=== High Dice Duel Begins! ===" );

			p1 = RollDie();
			p2 = RollDie();

			GameManager.AddLog( $"You rolled: [{p1}] [{p2}] (Total: {p1 + p2})" );
			GameManager.AddLog( "⚠️ AWAITING RE-ROLL DATA VECTOR INPUT..." );

			var diceEvent = new ChoiceEvent
			{
				Title = "🎲 HIGH DICE SYSTEM DUEL",
				PromptText = $"You rolled a [{p1}] and a [{p2}] (Current Total: {p1 + p2}). The AI is waiting for your final confirmation. Will you lock this score or execute a re-roll sub-routine?",
			};

			diceEvent.Options.Add( new EventOption
			{
				ButtonText = "🔒 KEEP CURRENT ROLL",
				ActionToExecute = () =>
				{
					GameManager.AddLog( "You choose to keep your current roll." );
					ResolveDiceDuel( p1, p2 );
				}
			} );

			diceEvent.Options.Add( new EventOption
			{
				ButtonText = "🎲 RE-ROLL DIE 1 ONLY",
				ActionToExecute = () =>
				{
					int newP1 = RollDie();
					GameManager.AddLog( $"Re-rolling Die 1... New roll: [{newP1}] [{p2}] (Total: {newP1 + p2})" );
					ResolveDiceDuel( newP1, p2 );
				}
			} );

			diceEvent.Options.Add( new EventOption
			{
				ButtonText = "🎲 RE-ROLL DIE 2 ONLY",
				ActionToExecute = () =>
				{
					int newP2 = RollDie();
					GameManager.AddLog( $"Re-rolling Die 2... New roll: [{p1}] [{newP2}] (Total: {p1 + newP2})" );
					ResolveDiceDuel( p1, newP2 );
				}
			} );

			diceEvent.Options.Add( new EventOption
			{
				ButtonText = "💥 RE-ROLL BOTH DICE",
				ActionToExecute = () =>
				{
					int newP1 = RollDie();
					int newP2 = RollDie();
					GameManager.AddLog( $"Re-rolling both dice... New roll: [{newP1}] [{newP2}] (Total: {newP1 + newP2})" );
					ResolveDiceDuel( newP1, newP2 );
				}
			} );

			manager.TriggerChoicePopup( diceEvent );
		}
	}

	private static void CheckMultiplayerResolution()
	{
		var manager = GetManager();
		if ( p1FinalTotal > 0 && p2FinalTotal > 0 && !isDuelResolved )
		{
			isDuelResolved = true;

			GameManager.AddLog( $"\n🎲 [DUEL OUTCOME] Both players have locked their selections!" );
			GameManager.AddLog( $"📊 {cachedPlayer.name} Final Total: {p1FinalTotal} | {cachedAI.name} Final Total: {p2FinalTotal}" );

			if ( p1FinalTotal > p2FinalTotal )
			{
				GameManager.AddLog( $"🏆 {cachedPlayer.name} WINS the dice duel! {cachedAI.name} loses 25 HP." );
				CombatManager.applyDamage( ref cachedAI, 25 );
			}
			else if ( p2FinalTotal > p1FinalTotal )
			{
				GameManager.AddLog( $"🏆 {cachedAI.name} WINS the dice duel! {cachedPlayer.name} loses 25 HP." );
				CombatManager.applyDamage( ref cachedPlayer, 25 );
			}
			else
			{
				GameManager.AddLog( $"🤝 It's a draw! Both players lose 15 HP." );
				CombatManager.applyDamage( ref cachedPlayer, 15 );
				CombatManager.applyDamage( ref cachedAI, 15 );
			}

			GameManager.AddLog( $"{cachedPlayer.name} | HP: {cachedPlayer.hp} | Armor: {cachedPlayer.defense}" );
			GameManager.AddLog( $"{cachedAI.name} | HP: {cachedAI.hp} | Armor: {cachedAI.defense}" );

			// Force-close the UI popup overlays across the game scene trees cleanly
			if ( manager != null ) manager.IsChoiceEventActive = false;
		}
	}

	private static void ResolveDiceDuel( int finalP1, int finalP2 )
	{
		var manager = GetManager();
		int prestige = manager != null ? manager.CurrentPrestige : 0;

		int scaledWinDamage = 25 + (prestige * 3);
		int scaledTieDamage = 15 + (prestige * 2);

		int playerTotal = finalP1 + finalP2;

		int a1 = RollDie();
		int a2 = RollDie();
		GameManager.AddLog( $"AI rolled: [{a1}] [{a2}] (Total: {a1 + a2})" );

		bool aiReroll = false;
		int currentAITotal = a1 + a2;

		if ( currentAITotal <= 6 )
			aiReroll = Random.Shared.Next( 1, 101 ) <= 70;
		else if ( currentAITotal <= 8 )
			aiReroll = Random.Shared.Next( 1, 101 ) <= 40;
		else
			aiReroll = Random.Shared.Next( 1, 101 ) <= 10;

		if ( aiReroll )
		{
			int rerollChoice = Random.Shared.Next( 1, 4 );

			if ( rerollChoice == 1 )
				a1 = RollDie();
			else if ( rerollChoice == 2 )
				a2 = RollDie();
			else
			{
				a1 = RollDie();
				a2 = RollDie();
			}

			GameManager.AddLog( $"AI re-rolled! Final roll: [{a1}] [{a2}] (Total: {a1 + a2})" );
		}
		else
		{
			GameManager.AddLog( "AI keeps its roll." );
		}

		int aiTotal = a1 + a2;

		if ( playerTotal > aiTotal )
		{
			GameManager.AddLog( $"You WIN the duel! AI loses {scaledWinDamage} HP." );
			CombatManager.applyDamage( ref cachedAI, scaledWinDamage );
		}
		else if ( aiTotal > playerTotal )
		{
			GameManager.AddLog( $"AI wins the duel! You lose {scaledWinDamage} HP." );
			CombatManager.applyDamage( ref cachedPlayer, scaledWinDamage );
		}
		else
		{
			GameManager.AddLog( $"It's a tie! Both lose {scaledTieDamage} HP." );
			CombatManager.applyDamage( ref cachedPlayer, scaledTieDamage );
			CombatManager.applyDamage( ref cachedAI, scaledTieDamage );
		}

		GameManager.AddLog( $"{cachedPlayer.name} | HP: {cachedPlayer.hp} | Armor: {cachedPlayer.defense}" );
		GameManager.AddLog( $"{cachedAI.name} | HP: {cachedAI.hp} | Armor: {cachedAI.defense}" );
	}
}