using UnityEngine;
using Unity.Netcode;

public class PlayerScoreComponent : NetworkBehaviour
{
    public int CurrentScore { get; private set; }

    public override void OnNetworkSpawn()
    {
        if (IsOwner && IsClient)
        {
            Debug.Log($"[PlayerScoreComponent] Requesting score initialization for Player {OwnerClientId}");
            ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId);
        }
        // For the server player (host), ensure the PlayerScoreComponent is registered properly
        if (IsServer && OwnerClientId == NetworkManager.Singleton.LocalClientId)
        {
            ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId);
        }
    }

    public void UpdateScore(int newScore)
    {
        CurrentScore = newScore;
        Debug.Log($"[PlayerScoreComponent] Player {OwnerClientId} score updated to {newScore}.");
        UpdateScoreUI();
    }

    private void UpdateScoreUI()
    {
        // Update the player's score display in the UI.
        Debug.Log($"[PlayerScoreComponent] Updated score UI for Player {OwnerClientId}: {CurrentScore}");
    }
}