using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CommonHelpers; // H…-types from DotNetHelpers library (with some DB-support + simple “ORM”)
using MyFlat.Base; // common types for MyFlat application (base exceptions etc.)

namespace MyFlat.DB {

//**************************************************************************************************
/// <summary>
/// Performing authorization request (via stored procedure call)
/// </summary>
public static class Authorization_SP {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public const string Name = "ЛК::Авторизация"; // authorization
public static readonly Guid
   //Status_ = SPCommonStatuses.,
   Status_ServiceNotAvailable    = new Guid( "71F0A024-228F-4199-B2B9-0C70D912D0F1" ),
   Status_AuthorizationNotPassed = new Guid( "7AD9B001-A3D2-4E3F-AD14-018D27742634" ),
   Status_AccessNotOpen          = new Guid( "0EDF636D-FDE1-4E07-9C15-6BB123B0DCF2" ),
   Status_AccountNotFound        = new Guid( "3BF5D1C7-AA30-410F-A9C0-358035C2A156" ),
   Status_NewRegistrationRequired= new Guid( "64F1A24C-8F10-4A39-A9ED-92E18DBB0751" );
   //Status_ = new Guid( "" ),
public static HSPResult_2<Authorization_Item,AccountInfo_Item>
   InvalidResult => new HSPResult_2<Authorization_Item,AccountInfo_Item>();
//==================================================================================================

public static async Task<HSPResult_2<Authorization_Item,AccountInfo_Item>>
   EXEC_A( long accountCode, string accessCode )
{
   using (new HSP(Name))
   {
      await HSP.Connect_A();
      var ( rp, cmd ) = HSP.PrepareCommand(
         HSqlParam.In( "КодЛС", accountCode ), // account ID
         HSqlParam.In( "КодДоступа", accessCode ) ); // user access code
      List<Authorization_Item>? list_1 = null;
      List<AccountInfo_Item>? list_2 = null;
      using (var rdr = await HSPCommandDispose.ExecuteReader_A( cmd ))
      if ( HSP.Success( rp ) )
      {
         list_1 = await HSP.RSetToList_A<Authorization_Item>( 1, rdr );
         if ( list_1.Count != 1 )
            throw HDBExceptionsSPAuto.ExactlyOneRecordWasExpectedIn1stRSet;
         if ( !rdr.NextResult() )
            throw HDBExceptionsSPAuto.SecondResultSetWasExpected;
         list_2 = await HSP.RSetToList_A<AccountInfo_Item>( 2, rdr );
         if ( list_2.Count != 1 )
            throw HDBExceptionsSPAuto.ExactlyOneRecordWasExpectedIn2ndRSet;
      }
      return new HSPResult_2<Authorization_Item,AccountInfo_Item>( rp, list_1, list_2 );
   }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // Authorization_SP
//**************************************************************************************************

/// <summary>
/// Model for 1-st result set
/// </summary>
public class Authorization_Item: ResultSetItemBase
{
   public Guid AccountId
      = GUID( "ИД ЛС" ); // account ID
   public long AccountCode
      = BIGINT( "Код ЛС" ); // account code (human readable/writable as dec.number)
   public DateTime RegistrationDate
      = DT( "Дата регистрации" ); // registration date (with time)
   public Authorization_Item Authorization_Duplicate() =>
      (Authorization_Item)MemberwiseClone();
} // Authorization_Item

/// <summary>
/// Model for 2-nd result set
/// </summary>
public class AccountInfo_Item: ResultSetItemBase
{
   public string Initials
      = NS( "ФИО" ); // flat owner initials
   public string Address
      = NS( "Адрес" ); // flat address
   public int PersonCount
      = INT( "Кол-во проживающих" ); // really live (person count)
   public decimal TotalSquare
      = DEC( "Общая площадь" ); // total flat square
   public decimal LivingSquare
      = DEC( "Жилая площадь" ); // living flat territory square
   public string DistrictName
      = NS( "Название района" ); // city district name
   public string TechsubdivOrgName
      = NS( "Название жил.орг." ); // name of technical subdivision org.
   public AccountInfo_Item AccountInfo_Duplicate() =>
      (AccountInfo_Item)MemberwiseClone();
} // AccountInfo_Item

//--------------------------------------------------------------------------------------------------
} // MyFlat.DB
