using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

using CommonHelpers;
using BookRegistry.Server.DB;

namespace BookRegistry.Server {

//**************************************************************************************************
partial class UserAuthorizationController {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[HttpPost("[action]")][Authorize(Roles=General_Role)]
public dynamic RefreshTokens_General( RefreshTokens_Args args ) =>
Do(args,()=>{ // RefreshTokens_Response
//-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   return GenerateTokens( false, args.GenerateBothTokens );
//-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
});

[HttpPost("[action]")][Authorize(Roles=Admin_Role)]
public dynamic RefreshTokens_Admin( RefreshTokens_Args args ) =>
Do(args,()=>{ // RefreshTokens_Response
//-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   return GenerateTokens( true, args.GenerateBothTokens );
//-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
});

//==================================================================================================

GeneralApiResponse GenerateTokens( bool forAdminRole, bool generateBothTokens )
{
   CON.PrintOperation( generateBothTokens ?
      "Recreating tokens (access & refresh)" :
      "Recreating access token" );
   string? userName = User.Identity.NN().Name;
   if ( userName == null )
      return GeneralApiResponse.GENERAL_FAILURE;
   string roleName = forAdminRole ? ServiceRoleNames.Administrator : ServiceRoleNames.General;
   return new RefreshTokens_Response( userName, roleName, generateBothTokens );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // UserAuthorizationController
//**************************************************************************************************

} // BookRegistry.Server
