using System.Security.Claims;
using System.Security.Principal;

namespace EnotaryoPH.Web.Common.Services
{
    public class CurrentUserService : ICurrentUserService
    {
        private readonly ClaimsIdentity _user;

        public CurrentUserService(IPrincipal principal) => _user = principal.Identity as ClaimsIdentity;

        public string? GetEmail() => _user.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value;

        public string? GetRole() => _user.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value;

        public Guid GetUser_UID()
        {
            if (_user == null)
            {
                return Guid.Empty;
            }

            var uid = _user.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;
            Guid.TryParse(uid, out var user_UID);
            return user_UID;
        }

        public bool IsAuthenticated() => _user?.IsAuthenticated ?? false;
    }
}