using UnityEngine;

public class Pickup : MonoBehaviour
{
    public PickupItemData itemData;
    public SpriteRenderer iconRenderer;
    private Interactable interactable;

    void Awake()
    {
        if (iconRenderer == null)
            iconRenderer = GetComponent<SpriteRenderer>();
        interactable = GetComponent<Interactable>();
        if (interactable != null)
        {
            interactable.Interacted += OnInteracted;
        }
        ApplyItemData();
    }

    void OnDestroy()
    {
        if (interactable != null)
        {
            interactable.Interacted -= OnInteracted;
        }
    }

#if UNITY_EDITOR
    void OnValidate()
    {
        if (iconRenderer == null)
            iconRenderer = GetComponent<SpriteRenderer>();
        ApplyItemData();
    }
#endif

    public void ApplyItemData()
    {
        if (itemData != null)
        {
            if (iconRenderer != null)
                iconRenderer.sprite = itemData.mapSprite;
            gameObject.name = itemData.itemName;
            // Optionally update other fields, e.g. description
        }
    }

    private void OnInteracted()
    {
        Debug.Log($"Pickup.OnInteracted: Picked up {itemData?.itemName}");
        // TODO: Add item to inventory manager here
        Destroy(gameObject);
    }
}
