using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// Defines all possible battle actions a character can take
/// </summary>
public enum BattleActionType
{
    Move,
    Attack,
    UseItem,
    CastSpell,
    RunAway,
    Defend, // Optional: block/defend action
    Wait     // Optional: skip turn but maintain position
}

/// <summary>
/// Enhanced action data to support multiple action types
/// </summary>
[System.Serializable]
public class EnhancedCharacterActionData
{
    [Header("Action State")]
    public ActionDecisionState state = ActionDecisionState.NoAction;
    public BattleActionType actionType = BattleActionType.Move;

    [Header("Targeting")]
    public Vector3 targetPosition = Vector3.zero;
    public GameObject targetEnemy;
    public Character targetCharacter; // For healing spells/items

    [Header("Item/Spell Data")]
    public string selectedItemName = "";
    public string selectedSpellName = "";
    public int selectedItemIndex = -1;

    [Header("Action Properties")]
    public bool requiresTarget = false;
    public bool isAreaOfEffect = false;
    public float aoeRadius = 0f;

    public bool hasTarget => targetEnemy != null || targetCharacter != null || targetPosition != Vector3.zero;
    public bool hasValidAction => state == ActionDecisionState.ActionSelected &&
                                 (actionType == BattleActionType.Wait ||
                                  actionType == BattleActionType.Defend ||
                                  actionType == BattleActionType.RunAway ||
                                  hasTarget ||
                                  (!string.IsNullOrEmpty(selectedItemName) && selectedItemIndex >= 0) ||
                                  !string.IsNullOrEmpty(selectedSpellName));

    public void Reset()
    {
        state = ActionDecisionState.NoAction;
        actionType = BattleActionType.Move;
        targetPosition = Vector3.zero;
        targetEnemy = null;
        targetCharacter = null;
        selectedItemName = "";
        selectedSpellName = "";
        selectedItemIndex = -1;
        requiresTarget = false;
        isAreaOfEffect = false;
        aoeRadius = 0f;
    }

    public void SetMoveAction(Vector3 position)
    {
        Reset();
        actionType = BattleActionType.Move;
        targetPosition = position;
        state = ActionDecisionState.ActionSelected;
    }

    public void SetAttackAction(GameObject enemy)
    {
        Reset();
        actionType = BattleActionType.Attack;
        targetEnemy = enemy;
        requiresTarget = true;
        state = ActionDecisionState.ActionSelected;
    }

    public void SetItemAction(string itemName, int itemIndex, bool requiresTargeting = false)
    {
        Reset();
        actionType = BattleActionType.UseItem;
        selectedItemName = itemName;
        selectedItemIndex = itemIndex;
        requiresTarget = requiresTargeting;
        state = requiresTargeting ? ActionDecisionState.NoAction : ActionDecisionState.ActionSelected;
    }

    public void SetSpellAction(string spellName, bool requiresTargeting = false, bool isAoE = false, float radius = 0f)
    {
        Reset();
        actionType = BattleActionType.CastSpell;
        selectedSpellName = spellName;
        requiresTarget = requiresTargeting;
        isAreaOfEffect = isAoE;
        aoeRadius = radius;
        state = requiresTargeting ? ActionDecisionState.NoAction : ActionDecisionState.ActionSelected;
    }

    public void SetDefendAction()
    {
        Reset();
        actionType = BattleActionType.Defend;
        state = ActionDecisionState.ActionSelected;
    }

    public void SetWaitAction()
    {
        Reset();
        actionType = BattleActionType.Wait;
        state = ActionDecisionState.ActionSelected;
    }

    public void SetRunAwayAction()
    {
        Reset();
        actionType = BattleActionType.RunAway;
        state = ActionDecisionState.ActionSelected;
    }

    public void SetTarget(GameObject enemy)
    {
        targetEnemy = enemy;
        if (requiresTarget)
        {
            state = ActionDecisionState.ActionSelected;
        }
    }

    public void SetTarget(Character character)
    {
        targetCharacter = character;
        if (requiresTarget)
        {
            state = ActionDecisionState.ActionSelected;
        }
    }

    public void SetTarget(Vector3 position)
    {
        targetPosition = position;
        if (requiresTarget && (isAreaOfEffect || actionType == BattleActionType.CastSpell))
        {
            state = ActionDecisionState.ActionSelected;
        }
    }

    public string GetActionDescription()
    {
        switch (actionType)
        {
            case BattleActionType.Move:
                return $"Move to {targetPosition}";
            case BattleActionType.Attack:
                return $"Attack {(targetEnemy ? targetEnemy.name : "target")}";
            case BattleActionType.UseItem:
                return $"Use {selectedItemName}";
            case BattleActionType.CastSpell:
                return $"Cast {selectedSpellName}";
            case BattleActionType.Defend:
                return "Defend";
            case BattleActionType.Wait:
                return "Wait";
            case BattleActionType.RunAway:
                return "Run Away";
            default:
                return "No Action";
        }
    }
}
