using UnityEngine;
using UnityEngine.SceneManagement;

/// <summary>
/// Spikes that reset the game when touched by a player.
/// Attach to a GameObject with a Collider2D set as Trigger.
/// </summary>
public class SpikeController : MonoBehaviour
{
    [Header("Settings")]
    [Tooltip("Delay before resetting (for death animation if needed)")]
    public float resetDelay = 0f;

    private bool isResetting = false;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        // Only react to players
        PlayerController player = collision.GetComponent<PlayerController>();
        if (player == null) return;

        // Prevent multiple resets
        if (isResetting) return;

        Debug.Log($"Player '{player.name}' hit spikes! Resetting level...");
        
        if (resetDelay > 0)
        {
            isResetting = true;
            Invoke(nameof(ResetLevel), resetDelay);
        }
        else
        {
            ResetLevel();
        }
    }

    private void ResetLevel()
    {
        // Reload the current scene
        Scene currentScene = SceneManager.GetActiveScene();
        SceneManager.LoadScene(currentScene.name);
    }
}
