using System;
#if WASM
   using CommonHelpers.WASM;
namespace BookRegistry.Client.DB {
#else // !WASM:
   using System.Threading.Tasks;
   using CommonHelpers;
namespace BookRegistry.Server.DB {
#endif // !WASM

//**************************************************************************************************
/// <summary>
/// User information extraction (stored procedure)
/// </summary>
public static class GetServiceUser_SP {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static readonly Guid
   //Status_ = SPCommonStatuses.,
   Status_EmptyLoginName = new Guid( "6A6CEF0F-A1D2-421D-B220-FB0F8FDB4C2F" );
   //Status_ = new Guid( "" ),
#if SERVER
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public const string Name = "BKRG::GetServiceUser";
public static ( HSPCommandResult, ServiceUser_Info? )
   InvalidResult => ( new HSPCommandResult(), null );
//==================================================================================================

public static async Task<(HSPCommandResult,ServiceUser_Info?)> EXEC_A(string loginName)
{
   using (new HSP(Name))
   {
      await HSP.Connect_A();
      var ( rp, cmd ) = HSP.PrepareCommand( HSqlParam.In( "LoginName", loginName ) );
      var list = await HSP.DoReaderCommandVerify_A<ServiceUser_Info>( rp, cmd );
      if ( list.Count > 1 )
         throw HDBExceptionsSPAuto.NotMoreThanOneRecordWasExpectedInRSet;
      return (
         new HSPResult<ServiceUser_Info>( rp, list ),
         HList.FirstOrNull( list ) );
   }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#endif // SERVER
} // GetServiceUser_SP
//**************************************************************************************************

/// <summary>
/// Service user model (result-set) with “server-only” password data
/// </summary>
public class ServiceUser_Info: ResultSetItemBase
{
   public string LoginName {
      get => Get( NLoginName );
      set => NLoginName = value; } internal string? NLoginName = _NS();

   public DateTime RegistrationUtc {
      get => Value( NRegistrationUtc );
      set => NRegistrationUtc = value; } internal DateTime? NRegistrationUtc = _UTC_( "CreationDate" );

   public string Initials {
      get => Get( NInitials );
      set => NInitials = value; } internal string? NInitials = _NS();

   public string? FirstName   { get; set; } = STR_N();
   public string? SecondName  { get; set; } = STR_N();

   public string LastName {
      get => Get( NLastName );
      set => NLastName = value; } internal string? NLastName = _NS();

   public bool IsAdministrator {
      get => Value( NIsAdministrator );
      set => NIsAdministrator = value; } internal bool? NIsAdministrator = _BOOL();

   public bool IsActiveUser {
      get => Value( NIsActiveUser );
      set => NIsActiveUser = value; } internal bool? NIsActiveUser = _BOOL();

#if SERVER
   internal byte[]? PasswordData { get; } = BINARY_N();
#endif // SERVER

#if WASM
   public DateTime RegistrationDate() => RegistrationUtc.ToLocalTime();
#endif // WASM

   public ServiceUser_Info ServiceUser_DuplicateInfo() =>
      (ServiceUser_Info)MemberwiseClone();
} // ServiceUser_Info

//==================================================================================================
} // BookRegistry.Client/Server.DB
