using Cortex.Mediator.Notifications;
using Cortex.Mediator.Queries;
using CortexMediatorDemo.API.Domain;
using Serilog;

namespace CortexMediatorDemo.API.CQRS.Queries;

public record BookByIdQuery(int Id) : IQuery<Book>, INotification;

/// <summary>
/// Represents a query handler responsible for processing a <see cref="BookByIdQuery"/>.
/// </summary>
/// <remarks>
/// The <c>BookByIdQueryHandler</c> retrieves a <see cref="Book"/> instance by its unique identifier.
/// </remarks>
public class BookByIdQueryHandler : IQueryHandler<BookByIdQuery, Book>
{
    public async Task<Book> Handle(BookByIdQuery query, CancellationToken cancellationToken)
    {
        try
        {
            //Log.Information("#{BookByIdQueryHandlerName} completed without errors!", nameof(BookByIdQueryHandler));

            // just a mock object...
            return new Book
            {
                Id = query.Id,
                Title = $"Book {query.Id}",
                Author = $"Author {query.Id}",
                Publisher = $"Publisher {query.Id}",
                Isbn = $"ISBN {query.Id}",
                Year = 2022,
                Language = "English",
                Description = $"Description {query.Id}",
                Category = $"Category {query.Id}"
            };
        }
        catch (Exception e)
        {
            Log.Error("#{BookByIdQueryHandlerName} caught exception: {EMessage}", nameof(BookByIdQueryHandler), e.Message);
            throw;
        }
        
        
    }
}




