using System.Security.Claims;

public class CurrentUserService : ICurrentUserService
{
    private readonly IHttpContextAccessor _http;

    public CurrentUserService(IHttpContextAccessor http) => _http = http;

    public Guid? UserId =>
        Guid.TryParse(_http.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier), out var id)
            ? id : null;

    public string? Username => _http.HttpContext?.User.FindFirstValue(ClaimTypes.Name);

    public string? DisplayName => _http.HttpContext?.User.FindFirstValue("display_name");

    public ClinicalRole? Role
    {
        get
        {
            var role = _http.HttpContext?.User.FindFirstValue("clinical_role");
            return role is null ? null : ClinicalRoleExtensions.FromDbString(role);
        }
    }

    public bool IsAuthenticated => _http.HttpContext?.User.Identity?.IsAuthenticated == true;

    public string? IpAddress => _http.HttpContext?.Connection.RemoteIpAddress?.ToString();
}