using UnityEngine;

/// <summary>
/// Force test the SettlementInteractionManager detection logic
/// </summary>
public class SettlementInteractionTest : MonoBehaviour
{
    [ContextMenu("Force Test Settlement Detection")]
    public void ForceTestSettlementDetection()
    {
        Debug.Log("=== FORCING SETTLEMENT INTERACTION MANAGER TEST ===");

        var settlementManager = FindFirstObjectByType<SettlementInteractionManager>();
        if (settlementManager == null)
        {
            Debug.LogError("❌ SettlementInteractionManager not found");
            return;
        }

        Debug.Log("✅ SettlementInteractionManager found");
        Debug.Log($"   GameObject: {settlementManager.gameObject.name}");
        Debug.Log($"   Enabled: {settlementManager.enabled}");
        Debug.Log($"   GameObject Active: {settlementManager.gameObject.activeInHierarchy}");

        // Get private fields using reflection to see current state
        var teamMarkerField = typeof(SettlementInteractionManager).GetField("teamMarker",
            System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        var mapDataField = typeof(SettlementInteractionManager).GetField("mapData",
            System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        var currentNearbySettlementField = typeof(SettlementInteractionManager).GetField("currentNearbySettlement",
            System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

        if (teamMarkerField != null)
        {
            var teamMarker = teamMarkerField.GetValue(settlementManager) as Transform;
            Debug.Log($"   TeamMarker reference: {(teamMarker != null ? teamMarker.name : "NULL")}");
        }

        if (mapDataField != null)
        {
            var mapData = mapDataField.GetValue(settlementManager);
            Debug.Log($"   MapData reference: {(mapData != null ? "EXISTS" : "NULL")}");
        }

        if (currentNearbySettlementField != null)
        {
            var currentSettlement = currentNearbySettlementField.GetValue(settlementManager);
            Debug.Log($"   Current nearby settlement: {(currentSettlement != null ? "EXISTS" : "NULL")}");
        }

        // Force call the CheckForSettlementInteraction method using reflection
        Debug.Log("\n🔍 FORCING CheckForSettlementInteraction()...");
        var checkMethod = typeof(SettlementInteractionManager).GetMethod("CheckForSettlementInteraction",
            System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

        if (checkMethod != null)
        {
            try
            {
                checkMethod.Invoke(settlementManager, null);
                Debug.Log("✅ CheckForSettlementInteraction() called successfully");
            }
            catch (System.Exception e)
            {
                Debug.LogError($"❌ Error calling CheckForSettlementInteraction(): {e.Message}");
            }
        }
        else
        {
            Debug.LogError("❌ CheckForSettlementInteraction method not found");
        }

        // Check state after forced call
        if (currentNearbySettlementField != null)
        {
            var currentSettlement = currentNearbySettlementField.GetValue(settlementManager);
            Debug.Log($"   Current nearby settlement AFTER: {(currentSettlement != null ? "EXISTS" : "NULL")}");
        }
    }
}
