using System.Security.Claims;

using Monitoring.Core.Domain.UserPrimitives;

namespace Monitoring.Common;

public static class ClaimsPrincipalExtensions
{
	public static UserId GetUserId(this ClaimsPrincipal principal)
	{
		var value = principal.FindFirstValue(ClaimTypes.NameIdentifier);
		return new(
			string.IsNullOrWhiteSpace(value)
				? throw new InvalidOperationException()
				: Guid.Parse(value)
		);
	}

	public static string GetUserRole(this ClaimsPrincipal principal)
	{
		return principal.FindFirstValue(ClaimTypes.Role)
			?? throw new InvalidOperationException();
	}
}