using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class BackButton : MonoBehaviour
{
    [Header("Target Scene")]
    public string targetSceneName = "TitleScreen";

    [Header("Button Reference")]
    public Button backButton;

    private void Awake()
    {
        // If no button is assigned, try to find it on this GameObject
        if (backButton == null)
        {
            backButton = GetComponent<Button>();
        }

        // If still no button found, try to find one in children
        if (backButton == null)
        {
            backButton = GetComponentInChildren<Button>();
        }

        if (backButton != null)
        {
            backButton.onClick.AddListener(GoBack);
        }
        else
        {
            Debug.LogWarning("No Button component found for BackButton script on " + gameObject.name);
        }
    }

    private void GoBack()
    {
        Debug.Log($"Going back to scene: {targetSceneName}");
        SceneManager.LoadScene(targetSceneName);
    }

    // Method to set target scene programmatically
    public void SetTargetScene(string sceneName)
    {
        targetSceneName = sceneName;
    }
}
