using System;
using System.Text;
using Microsoft.IdentityModel.Tokens;

using CommonHelpers;

namespace BookRegistry.Server
{
   enum AuthTokenType
   {
      Access, Refresh
   }
   static class AuthOptions
   {
      public static readonly string
         Issuer = Program.AppModuleBaseName, // "BookRegistry.Server"
         Audience = "BookRegistry.Client", // WASM-Blazor client
         Key = Guid.NewGuid().ToString(); // auto-generated key (string)
      public static readonly TimeSpan
         LifetimePeriod_Access = new TimeSpan( 0, 5, 0 ), // minutes for access token
         LifetimePeriod_Refresh = new TimeSpan( 1, 0, 0 ); // hours for refresh token
      public static SymmetricSecurityKey GetSymmetricSecurityKey() =>
         new SymmetricSecurityKey( Encoding.ASCII.GetBytes( Key ) );
      public static TimeSpan GetLifetimePeriod(bool refreshTokenType) =>
         GetLifetimePeriod(refreshTokenType ? AuthTokenType.Refresh : AuthTokenType.Access);
      public static TimeSpan GetLifetimePeriod(AuthTokenType tokenType)
      {
         switch ( tokenType )
         {
         case AuthTokenType.Access: return LifetimePeriod_Access;
         case AuthTokenType.Refresh: return LifetimePeriod_Refresh;
         default: throw HAssertionFailure.DEAD_BRANCH();
         }
      }
   }
   class JwtInfo
   {
      public string EncodedJwt { get; }
      public string IdentityName { get; }
      public string RoleName { get; }
      public DateTime ExpirationUtc { get; }
      public JwtInfo( string encodedJwt,
                      string identityName, string roleName, DateTime expirationUtc )
      {
         EncodedJwt = encodedJwt;
         IdentityName = identityName;
         RoleName = roleName;
         ExpirationUtc = expirationUtc;
      }
   }
} // BookRegistry.Server
