using UnityEngine;

[ExecuteInEditMode]
public class LayerAssigner : MonoBehaviour
{
    [Header("Auto-assign layers")]
    [SerializeField] private bool autoAssign = true;

    void Start()
    {
        if (autoAssign)
        {
            AssignLayers();
        }
    }

    [ContextMenu("Assign Layers Now")]
    public void AssignLayers()
    {
        // Assign all players to Player layer (6)
        PlayerController[] players = FindObjectsByType<PlayerController>(FindObjectsSortMode.None);
        foreach (var player in players)
        {
            player.gameObject.layer = LayerMask.NameToLayer("Player");
            Debug.Log($"Assigned {player.stats.playerName} to Player layer");
        }

        // Assign puck to Puck layer (7)
        PuckController puck = FindFirstObjectByType<PuckController>();
        if (puck != null)
        {
            puck.gameObject.layer = LayerMask.NameToLayer("Puck");
            Debug.Log("Assigned Puck to Puck layer");
        }

        // Find rink objects (anything with "Rink" or "Ice" in name)
        GameObject[] allObjects = FindObjectsByType<GameObject>(FindObjectsSortMode.None);
        foreach (var obj in allObjects)
        {
            if (obj.name.ToLower().Contains("rink") || obj.name.ToLower().Contains("ice"))
            {
                obj.layer = LayerMask.NameToLayer("Rink");
                Debug.Log($"Assigned {obj.name} to Rink layer");
            }
        }

        Debug.Log("Layer assignment complete!");
    }
}