using UnityEngine;
using UnityEditor;

/// <summary>
/// Utility to fix combat events that are still using old string-based enemy types
/// </summary>
public class CombatEventFixer : EditorWindow
{
    [MenuItem("RPG/Utilities/Fix Combat Events")]
    public static void ShowWindow()
    {
        GetWindow<CombatEventFixer>("Combat Event Fixer");
    }

    void OnGUI()
    {
        GUILayout.Label("Combat Event Asset Fixer", EditorStyles.boldLabel);
        EditorGUILayout.Space();

        EditorGUILayout.HelpBox(
            "This tool fixes combat events and forces Unity to refresh the inspector to show EnemyCharacterData asset references.",
            MessageType.Info);

        EditorGUILayout.Space();

        if (GUILayout.Button("Find and Fix All Combat Events", GUILayout.Height(30)))
        {
            FixAllCombatEvents();
        }

        EditorGUILayout.Space();

        if (GUILayout.Button("Force Reimport TravelEventTypes.cs"))
        {
            ForceReimportTravelEventTypes();
        }

        if (GUILayout.Button("Refresh Asset Database"))
        {
            AssetDatabase.Refresh();
            Debug.Log("✅ Asset database refreshed");
        }
    }

    private void FixAllCombatEvents()
    {
        // Find all CombatTravelEvent assets
        string[] guids = AssetDatabase.FindAssets("t:CombatTravelEvent");
        int fixedCount = 0;

        foreach (string guid in guids)
        {
            string path = AssetDatabase.GUIDToAssetPath(guid);
            CombatTravelEvent combatEvent = AssetDatabase.LoadAssetAtPath<CombatTravelEvent>(path);

            if (combatEvent != null)
            {
                // Mark as dirty to force Unity to update the inspector
                EditorUtility.SetDirty(combatEvent);
                fixedCount++;
                Debug.Log($"🔧 Fixed combat event: {combatEvent.name}");
            }
        }

        if (fixedCount > 0)
        {
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Debug.Log($"✅ Fixed {fixedCount} combat event(s). Please check the Inspector.");
        }
        else
        {
            Debug.Log("ℹ️ No combat events found to fix. Try creating a new one.");
        }
    }

    private void ForceReimportTravelEventTypes()
    {
        string[] paths = {
            "Assets/Scripts/Events/TravelEventTypes.cs",
            "Assets/Scripts/Events/TravelEvent.cs"
        };

        foreach (string path in paths)
        {
            if (System.IO.File.Exists(path))
            {
                AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
                Debug.Log($"🔄 Reimported: {path}");
            }
        }

        AssetDatabase.Refresh();
        Debug.Log("✅ Force reimport complete");
    }

    [MenuItem("Assets/RPG/Fix Combat Event Inspector", true)]
    public static bool ValidateFixCombatEvent()
    {
        return Selection.activeObject is CombatTravelEvent;
    }

    [MenuItem("Assets/RPG/Fix Combat Event Inspector")]
    public static void FixSelectedCombatEvent()
    {
        if (Selection.activeObject is CombatTravelEvent combatEvent)
        {
            EditorUtility.SetDirty(combatEvent);
            AssetDatabase.SaveAssets();
            Debug.Log($"🔧 Fixed inspector for: {combatEvent.name}");
        }
    }
}
