using BookHive.Models;
using BookHive.Repositories;
using BookHive.Specifications.Reviews;
using FastEndpoints;

namespace BookHive.Endpoints.Reviews;

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

public class DeleteReviewEndpoint(ReviewRepository reviewRepository) : Endpoint<DeleteReviewsRequest>
{
    public override void Configure()
    {
        Delete("/reviews/{@Id}", x => new { x.Id });
        AllowAnonymous();
    }

    public override async Task HandleAsync(DeleteReviewsRequest req, CancellationToken ct)
    {
        Review? review = await reviewRepository.SingleOrDefaultAsync(new GetReviewByIdSpec(req.Id), ct);

        if (review is null)
        {
            await Send.NotFoundAsync(ct);
            return;
        }

        await reviewRepository.DeleteAsync(review, ct);
        await Send.NoContentAsync(ct);
    }
}