using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using TeleWave.Application.Common.Interfaces;

namespace TeleWave.Infrastructure.Identity;

internal sealed class CurrentUser(IHttpContextAccessor httpContextAccessor) : ICurrentUser
{
    private ClaimsPrincipal? Principal => httpContextAccessor.HttpContext?.User;

    public Guid? UserId => ParseHttpUserId();

    private Guid? ParseHttpUserId()
    {
        var value = Principal?.FindFirstValue(ClaimTypes.NameIdentifier);
        return Guid.TryParse(value, out var id) ? id : null;
    }
}
