using UnityEngine;

public class GameSetup : MonoBehaviour
{
    [Header("Auto Setup")]
    [SerializeField] private bool setupOnAwake = true;
    [SerializeField] private bool createLayersIfMissing = true;

    void Awake()
    {
        if (setupOnAwake)
        {
            SetupPhysicsLayers();
            AssignLayers();
        }
    }

    void SetupPhysicsLayers()
    {
        // Layer 6 = Player
        // Layer 7 = Puck
        // Layer 8 = Rink

        // Puck passes through players (we'll add interception logic separately)
        Physics.IgnoreLayerCollision(6, 7, true); // Player <-> Puck = ignore collision

        // Players still collide with each other
        Physics.IgnoreLayerCollision(6, 6, false); // Player <-> Player = collide

        // Players collide with rink
        Physics.IgnoreLayerCollision(6, 8, false); // Player <-> Rink = collide

        // Puck collides with rink
        Physics.IgnoreLayerCollision(7, 8, false); // Puck <-> Rink = collide

        Debug.Log("✓ Physics layers configured: Puck will pass through players");
    }

    void AssignLayers()
    {
        int playerLayer = LayerMask.NameToLayer("Player");
        int puckLayer = LayerMask.NameToLayer("Puck");
        int rinkLayer = LayerMask.NameToLayer("Rink");

        // Check if layers exist
        if (playerLayer == -1 || puckLayer == -1 || rinkLayer == -1)
        {
            Debug.LogWarning("⚠ Layers not found! Please create them manually:");
            Debug.LogWarning("Edit → Project Settings → Tags and Layers");
            Debug.LogWarning("Set Layer 6 = 'Player', Layer 7 = 'Puck', Layer 8 = 'Rink'");
            return;
        }

        // Assign all players to Player layer (6)
        PlayerController[] players = FindObjectsByType<PlayerController>(FindObjectsSortMode.None);
        foreach (var player in players)
        {
            SetLayerRecursively(player.gameObject, playerLayer);
        }
        Debug.Log($"✓ Assigned {players.Length} players to Player layer");

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

        // Find rink objects (anything with "Rink", "Ice", "Board" in name)
        GameObject[] allObjects = FindObjectsByType<GameObject>(FindObjectsSortMode.None);
        int rinkCount = 0;
        foreach (var obj in allObjects)
        {
            string nameLower = obj.name.ToLower();
            if (nameLower.Contains("rink") || nameLower.Contains("ice") ||
                nameLower.Contains("board") || nameLower.Contains("surface"))
            {
                SetLayerRecursively(obj, rinkLayer);
                rinkCount++;
            }
        }
        Debug.Log($"✓ Assigned {rinkCount} rink objects to Rink layer");
    }

    void SetLayerRecursively(GameObject obj, int layer)
    {
        obj.layer = layer;
        foreach (Transform child in obj.transform)
        {
            SetLayerRecursively(child.gameObject, layer);
        }
    }

    [ContextMenu("Setup Now")]
    public void SetupNow()
    {
        SetupPhysicsLayers();
        AssignLayers();
    }
}