using System.Security.Claims;
using eSuite.Core.Miscellaneous;

namespace eSuite.API.Utilities;

/// <summary>
/// These methods are used to help make working with authenticated users easier
/// </summary>
internal static class ClaimsPrincipalExtensions
{
    /// <summary>
    /// This gets the e-mail address of the current user.
    /// </summary>
    /// <param name="user"></param>
    /// <returns>email address from the JWT Token</returns>
    public static string Email(this ClaimsPrincipal user)
    {
        return user.FindFirst(ClaimTypes.Email)?.Value!;
    }

    /// <summary>
    /// Gets the Internal DB user Id of the current user.
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    public static long Id(this ClaimsPrincipal user)
    {
        return long.Parse(user.FindFirst(ClaimTypes.PrimarySid)?.Value!);
    }

    public static GeneralIdRef GeneralIdRef(this ClaimsPrincipal user)
    {
        return new GeneralIdRef
        {
            Id = user.Id()
        };
    }

    /// <summary>
    /// Gets the Display name of the user as the time the Token was generated.
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    public static string DisplayName(this ClaimsPrincipal user)
    {
        return user.FindFirst(ClaimTypes.Name)?.Value!;
    }
}