using UnityEngine;
using TMPro;

public class ItemShop : MonoBehaviour
{
    [Header("Item Configuration")]
    public int itemID;                             // Unique ID for the item
    public int itemPrice;                          // Price of the item
    public GameObject itemObject;                  // Object to enable upon purchase
    public GameObject assignedObjectToDisable;     // Object to disable upon purchase (e.g., Purchase Button)
    public TextMeshProUGUI feedbackText;           // Feedback text for the user

    private MoneySaver moneySaver;                 // Reference to MoneySaver for total money

    private void Awake()
    {
        // Ensure MoneySaver is available
        moneySaver = FindObjectOfType<MoneySaver>();
        if (moneySaver == null)
        {
            Debug.LogError("MoneySaver instance not found in the scene.");
        }
    }

    private void Start()
    {
        InitializeItemState();
    }

    /// <summary>
    /// Initializes the item's active state based on purchase status.
    /// Ensures all items are managed via script.
    /// </summary>
    public void InitializeItemState()
    {
        bool isPurchased = PlayerPrefs.GetInt("PurchasedItem_" + itemID, 0) == 1;

        if (isPurchased)
        {
            EnableItem();
            HideAssignedObject();
            if (feedbackText != null)
            {
                feedbackText.text = "Item Purchased";
            }
        }
        else
        {
            DisableItem();
            ShowAssignedObject();
            if (feedbackText != null)
            {
                feedbackText.text = "Price: $" + itemPrice;
            }
        }
    }

    /// <summary>
    /// Attempts to purchase the item.
    /// </summary>
    public void AttemptPurchase()
    {
        if (moneySaver == null)
        {
            Debug.LogError("MoneySaver reference is missing.");
            return;
        }

        int currentMoney = PlayerPrefs.GetInt("TotalMoney", 0);

        // Check if the item is already purchased
        if (PlayerPrefs.GetInt("PurchasedItem_" + itemID, 0) == 1)
        {
            if (feedbackText != null)
            {
                feedbackText.text = "Already Purchased!";
            }
            return;
        }

        // Check if the player has enough money
        if (currentMoney >= itemPrice)
        {
            int updatedMoney = currentMoney - itemPrice;
            PlayerPrefs.SetInt("TotalMoney", updatedMoney);
            PlayerPrefs.Save();

            // Update MoneySaver UI
            moneySaver.UpdateTotalMoneyUI(updatedMoney);

            // Mark the item as purchased
            PlayerPrefs.SetInt("PurchasedItem_" + itemID, 1);
            PlayerPrefs.Save();

            // Enable the purchased item and disable the purchase button
            EnableItem();
            HideAssignedObject();

            if (feedbackText != null)
            {
                feedbackText.text = "Purchase Successful!";
            }
        }
        else
        {
            if (feedbackText != null)
            {
                feedbackText.text = "Not Enough Money!";
            }
        }
    }

    /// <summary>
    /// Enables the itemObject.
    /// </summary>
    private void EnableItem()
    {
        if (itemObject != null)
        {
            itemObject.SetActive(true);
        }
        else
        {
            Debug.LogWarning($"ItemObject is not assigned for itemID {itemID}");
        }
    }

    /// <summary>
    /// Disables the itemObject.
    /// </summary>
    private void DisableItem()
    {
        if (itemObject != null)
        {
            itemObject.SetActive(false);
        }
        else
        {
            Debug.LogWarning($"ItemObject is not assigned for itemID {itemID}");
        }
    }

    /// <summary>
    /// Hides the assigned object (e.g., Purchase Button).
    /// </summary>
    private void HideAssignedObject()
    {
        if (assignedObjectToDisable != null)
        {
            assignedObjectToDisable.SetActive(false);
        }
    }

    /// <summary>
    /// Shows the assigned object (e.g., Purchase Button).
    /// </summary>
    private void ShowAssignedObject()
    {
        if (assignedObjectToDisable != null)
        {
            assignedObjectToDisable.SetActive(true);
        }
    }

    /// <summary>
    /// Ensures the item's state is correctly set when the object is enabled.
    /// </summary>
    private void OnEnable()
    {
        InitializeItemState();
    }
}

Enable the itemObject variable on scene load and save the state to make it be enabled always