namespace Brizco.Api.Controllers;

public class NotificationController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("Notifications")
            .MapGroup("api/notification");

        group.MapGet("", GetUserNotificationsAsync)
            .WithDisplayName("Get User Notifications")
            .WithDescription("Get user notifications , by JWT user id")
            .RequireAuthorization(builder=>builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser())
            .HasApiVersion(1.0);

        group.MapGet("/unread/count", GetUserNotificationsCountAsync)
            .WithDisplayName("Get Unread Notifications Count")
            .WithDescription("Get user notifications , by JWT user id")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser())
            .HasApiVersion(1.0);

        group.MapPost("{id}/read",ReadNotificationAsync)
            .WithDisplayName("Read Notification")
            .WithDescription("Read notification , by JWT user id")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser())
            .HasApiVersion(1.0);

    }

    private async Task<IResult> ReadNotificationAsync([FromRoute] Guid id, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new ReadNotificationCommand(id), cancellationToken));

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

    private async Task<IResult> GetUserNotificationsCountAsync([FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetNotificationsCountQuery(), cancellationToken));
}