using UnityEngine;

/// <summary>
/// Detailed diagnostic script to check what components are actually on GameObjects
/// </summary>
public class SettlementGameObjectDiagnostic : MonoBehaviour
{
    [ContextMenu("Diagnose Settlement GameObjects")]
    public void DiagnoseGameObjects()
    {
        Debug.Log("=== SETTLEMENT GAMEOBJECT DIAGNOSTIC ===");

        // Find all GameObjects with settlement-related names
        var allObjects = FindObjectsByType<GameObject>(UnityEngine.FindObjectsSortMode.None);

        foreach (var obj in allObjects)
        {
            if (obj.name.Contains("Settlement") || obj.name.Contains("settlement"))
            {
                Debug.Log($"\n🎯 GameObject: '{obj.name}'");
                Debug.Log($"   Active: {obj.activeInHierarchy}");
                Debug.Log($"   Tag: {obj.tag}");

                // List all components
                var components = obj.GetComponents<Component>();
                Debug.Log($"   Components ({components.Length}):");
                foreach (var comp in components)
                {
                    if (comp != null)
                    {
                        Debug.Log($"     - {comp.GetType().Name}");
                    }
                }
            }
        }

        Debug.Log("\n=== SPECIFIC COMPONENT SEARCH ===");

        // Search for specific components
        var settlementManager = FindFirstObjectByType<SettlementInteractionManager>();
        var settlementUI = FindFirstObjectByType<SettlementInteractionUI>();
        var settlementBridge = FindFirstObjectByType<SettlementUIBridge>();

        Debug.Log($"SettlementInteractionManager found: {settlementManager != null}");
        if (settlementManager != null)
        {
            Debug.Log($"   On GameObject: '{settlementManager.gameObject.name}'");
        }

        Debug.Log($"SettlementInteractionUI found: {settlementUI != null}");
        if (settlementUI != null)
        {
            Debug.Log($"   On GameObject: '{settlementUI.gameObject.name}'");
        }

        Debug.Log($"SettlementUIBridge found: {settlementBridge != null}");
        if (settlementBridge != null)
        {
            Debug.Log($"   On GameObject: '{settlementBridge.gameObject.name}'");
            Debug.Log($"   Settlement UI Reference: {settlementBridge.settlementUI != null}");
        }

        Debug.Log("\n=== UI DOCUMENT SEARCH ===");

        // Check for UIDocument components
        var uiDocuments = FindObjectsByType<UnityEngine.UIElements.UIDocument>(UnityEngine.FindObjectsSortMode.None);
        Debug.Log($"Found {uiDocuments.Length} UIDocument components:");

        foreach (var uiDoc in uiDocuments)
        {
            Debug.Log($"   UIDocument on '{uiDoc.gameObject.name}'");
            Debug.Log($"     Asset: {(uiDoc.visualTreeAsset != null ? uiDoc.visualTreeAsset.name : "NULL")}");
        }
    }
}
