using BookHive.DTO.Author;
using BookHive.Models;
using BookHive.Repositories;
using BookHive.Specifications.Authors;
using FastEndpoints;

namespace BookHive.Endpoints.Authors;

public class AuthorRequest
{
    public int Id { get; set; }
}

public class GetAuthorEndpoint(AuthorRepository authorRepository, AutoMapper.IMapper mapper) : Endpoint<AuthorRequest, GetAuthorDetailsDto>
{
    public override void Configure()
    {
        Get("/authors/{@Id}/", x => new { x.Id });
        AllowAnonymous();
    }

    public override async Task HandleAsync(AuthorRequest req, CancellationToken ct)
    {
        Author? author = await authorRepository.SingleOrDefaultAsync(new GetAuthorByIdSpec(req.Id), ct);

        if (author is null)
        {
            await Send.NoContentAsync(ct);
            return;
        }

        await Send.OkAsync(mapper.Map<GetAuthorDetailsDto>(author), ct);
    }
}