namespace Brizco.Api.Controllers;

public class NewsFeedController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("News")
            .MapGroup("api/news");

        group.MapGet("", GetEntitiesAsync)
            .WithDisplayName("Get News Feed")
            .HasApiVersion(1.0);

        group.MapPost("", PostEntitiesAsync)
            .WithDisplayName("Create News Feed")
            .HasApiVersion(1.0);

        group.MapPut("{id}/pin", ChangePinAsync)
            .WithDisplayName("Change NewsFeed Pin")
            .HasApiVersion(1.0);

        group.MapDelete("{id}", DeleteEntitiesAsync)
            .WithDisplayName("Delete News Feed")
            .HasApiVersion(1.0);
    }

    private async Task GetEntitiesAsync([FromQuery] int page, [FromQuery] int? count, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetNewsFeedsQuery(page, count), cancellationToken));

    private async Task PostEntitiesAsync([FromBody]CreateNewsFeedCommand request, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(request, cancellationToken));

    private async Task ChangePinAsync([FromRoute] Guid id, [FromQuery]bool isPin, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new ChangeNewsFeedPinCommand(id,isPin), cancellationToken));

    private async Task DeleteEntitiesAsync([FromRoute] Guid id, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new DeleteNewsFeedCommand(id), cancellationToken));
}