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

//**************************************************************************************************
/// <summary>
/// Book list selection (stored procedure)
/// </summary>
public static class SelectBooks_SP {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static readonly Guid
   //Status_ = SPCommonStatuses.,
   Status_NothingSpecified = SPCommonStatuses.NothingSpecified,
   Status_IncorrectSortingKey = new Guid( "F6DB1383-8559-45B0-9844-7E762E7AE3F9" );
   //Status_ = new Guid( "" ),
#if SERVER
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public const string Name = "BKRG::SelectBooks";
public static HSPResult<SelectBooks_Item>
   InvalidResult => new HSPResult<SelectBooks_Item>();
//==================================================================================================

public static async Task<HSPResult<SelectBooks_Item>> EXEC_A(SelectBooks_Input input)
{
   using (new HSP(Name))
   {
      await HSP.Connect_A();
      var ( rp, cmd ) = input.SelectBooks_PrepareCommand();
      var list = await HSP.DoReaderCommand_A<SelectBooks_Item>( cmd );
      return new HSPResult<SelectBooks_Item>( rp, list );
   }
}

#endif // SERVER
} // SelectBooks_SP
//**************************************************************************************************

/// <summary>
/// Input parameters model
/// </summary>
public class SelectBooks_Input: ApiArgumentsBase
{
   public string? NamePart { get; set; }
   public string? ISBNPart { get; set; }
   public Guid? AuthorId { get; set; }
   public Guid? CategoryId { get; set; }
   public Guid? GenreId { get; set; }
   public Guid? PublisherId { get; set; }
   public Guid? SeriesId { get; set; }
   public int? ReleaseYear_From { get; set; }
   public int? ReleaseYear_To { get; set; }
   public string? SortingKey { get; set; }
#if SERVER
   internal ( HSPResultParameters, SqlCommand ) SelectBooks_PrepareCommand() =>
      HSP.PrepareCommand(
         HSqlParam.NullableIn( nameof(NamePart), NamePart ),
         HSqlParam.NullableIn( nameof(ISBNPart), ISBNPart ),
         HSqlParam.NullableIn( nameof(AuthorId), AuthorId ),
         HSqlParam.NullableIn( nameof(CategoryId), CategoryId ),
         HSqlParam.NullableIn( nameof(GenreId), GenreId ),
         HSqlParam.NullableIn( nameof(PublisherId), PublisherId ),
         HSqlParam.NullableIn( nameof(SeriesId), SeriesId ),
         HSqlParam.NullableIn( nameof(ReleaseYear_From), ReleaseYear_From ),
         HSqlParam.NullableIn( nameof(ReleaseYear_To), ReleaseYear_To ),
         HSqlParam.NullableIn( nameof(SortingKey), SortingKey ) );
#endif // SERVER
} // SelectBooks_Input

//--------------------------------------------------------------------------------------------------

/// <summary>
/// Result set (model), is derived from “IdName_Item” ([ID]+[Name] fields)
/// </summary>
public class SelectBooks_Item: ResultSetItemBase
{
#if WASM
   internal bool deleted,corrected;
#endif // WASM
   public Guid ID {
      get => Value( NID );
      set => NID = value; } internal Guid? NID = _GUID();

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

   public string? ISBN { get; set; } = STR_N();
   public int? ReleaseYear { get; set; } = INT_N();
   public string? Description { get; set; } = STR_N();

   public Guid AuthorId {
      get => Value( NAuthorId );
      set => NAuthorId = value; } internal Guid? NAuthorId = _GUID();
   public string AuthorName {
      get => Get( NAuthorName );
      set => NAuthorName = value; } internal string? NAuthorName = _NS();

   public Guid PublisherId {
      get => Value( NPublisherId );
      set => NPublisherId = value; } internal Guid? NPublisherId = _GUID();
   public string PublisherName {
      get => Get( NPublisherName );
      set => NPublisherName = value; } internal string? NPublisherName = _NS();

   public Guid SeriesId {
      get => Value( NSeriesId );
      set => NSeriesId = value; } internal Guid? NSeriesId = _GUID();
   public string SeriesName {
      get => Get( NSeriesName );
      set => NSeriesName = value; } internal string? NSeriesName = _NS();

   public Guid CategoryId {
      get => Value( NCategoryId );
      set => NCategoryId = value; } internal Guid? NCategoryId = _GUID();
   public string CategoryName {
      get => Get( NCategoryName );
      set => NCategoryName = value; } internal string? NCategoryName = _NS();

   public Guid GenreId {
      get => Value( NGenreId );
      set => NGenreId = value; } internal Guid? NGenreId = _GUID();
   public string GenreName {
      get => Get( NGenreName );
      set => NGenreName = value; } internal string? NGenreName = _NS();

   public SelectBooks_Item SelectBooks_DuplicateItem() =>
      (SelectBooks_Item)MemberwiseClone();
} // SelectBooks_Item

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