using MessagePipe;
using Players;
using UnityEngine;
using VContainer;
using VContainer.Unity;

public sealed class ApplicationLifetimeScope : LifetimeScope
{
    [SerializeField] private PlayerNameService _playerNameService;
    
    protected override void Configure(IContainerBuilder builder)
    {
        RegisterMessagePipe(builder);
        RegisterServices(builder);
    }

    private void RegisterMessagePipe(IContainerBuilder builder)
    {
#if UNITY_EDITOR
        // Enable stack trace capture for diagnostics
        builder.RegisterMessagePipe(options =>
        {
            options.EnableCaptureStackTrace = true;
        });

        // Set the global MessagePipe provider
        builder.RegisterBuildCallback(container =>
        {
            GlobalMessagePipe.SetProvider(container.AsServiceProvider());
        });
#else
            builder.RegisterMessagePipe();
#endif
    }

    private void RegisterServices(IContainerBuilder builder)
    {
        builder.RegisterInstance(_playerNameService).AsSelf();
    }
}