using UnityEngine;

/// <summary>
/// Quick setup script to add all necessary travel system components to the current scene.
/// Run this from the context menu or attach it to a GameObject.
/// </summary>
public class QuickTravelSystemSetup : MonoBehaviour
{
    [ContextMenu("Setup Travel System")]
    public void SetupTravelSystem()
    {
        Debug.Log("🔧 Setting up Travel System...");

        // Find or create main components
        SetupTeamTravelSystem();
        SetupTravelDebugger();

        Debug.Log("✅ Travel System setup complete!");
        Debug.Log("💡 Use F8 to toggle travel system debug logs");
        Debug.Log("💡 Use F9 to toggle debug overlay");
        Debug.Log("💡 Click on the map to plan travel routes");
    }

    private void SetupTeamTravelSystem()
    {
        TeamTravelSystem travelSystem = FindFirstObjectByType<TeamTravelSystem>();

        if (travelSystem == null)
        {
            GameObject travelObj = new GameObject("TeamTravelSystem");
            travelSystem = travelObj.AddComponent<TeamTravelSystem>();
            Debug.Log("✅ Created TeamTravelSystem");
        }
        else
        {
            Debug.Log("🔍 Found existing TeamTravelSystem");
        }

        // Enable debug logs by default
        travelSystem.showDebugLogs = true;
        Debug.Log("✅ Enabled travel system debug logs");
    }

    private void SetupTravelDebugger()
    {
        TravelSystemDebugger debugger = FindFirstObjectByType<TravelSystemDebugger>();

        if (debugger == null)
        {
            GameObject debugObj = new GameObject("TravelSystemDebugger");
            debugger = debugObj.AddComponent<TravelSystemDebugger>();
            Debug.Log("✅ Created TravelSystemDebugger");
        }
        else
        {
            Debug.Log("🔍 Found existing TravelSystemDebugger");
        }

        // Enable debug overlay by default
        debugger.enableDebugOverlay = true;
        Debug.Log("✅ Enabled debug overlay");
    }

    [ContextMenu("Test Travel System")]
    public void TestTravelSystem()
    {
        Debug.Log("🧪 Testing Travel System Components...");

        var mapMaker = FindFirstObjectByType<MapMaker2>();
        var teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
        var travelSystem = FindFirstObjectByType<TeamTravelSystem>();

        Debug.Log($"MapMaker2: {(mapMaker != null ? "✅ Found" : "❌ Missing")}");
        Debug.Log($"SimpleTeamPlacement: {(teamPlacement != null ? "✅ Found" : "❌ Missing")}");
        Debug.Log($"TeamTravelSystem: {(travelSystem != null ? "✅ Found" : "❌ Missing")}");

        if (teamPlacement != null)
        {
            Debug.Log($"Team Placed: {(teamPlacement.IsTeamPlaced() ? "✅ Yes" : "❌ No")}");
            if (teamPlacement.IsTeamPlaced())
            {
                Debug.Log($"Team Position: {teamPlacement.GetTeamPosition()}");
            }
        }

        if (mapMaker?.GetMapData() != null)
        {
            var mapData = mapMaker.GetMapData();
            Debug.Log($"Map Size: {mapData.Width}x{mapData.Height}");
            Debug.Log($"Tile Size: {mapMaker.mapVisualizer?.tileSize ?? 1f}");
        }

        Debug.Log("🧪 Test complete");
    }
}
