namespace Netina.Api.Controllers;

public class FaqController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("Faq")
            .MapGroup("api/faq");

        group.MapGet("/slug", GetFaqBySlugAsync)
            .WithDisplayName("Get Faq By Slug")
            .WithDescription("Get faq by slug , you have to send page slug")
            .HasApiVersion(1.0);

        group.MapGet("", GetFaqsAsync)
            .WithDisplayName("Get Faqs")
            .WithDescription("Get All Faqs")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer")
                .RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageFaq))
            .HasApiVersion(1.0);

        group.MapPost("", CreateFaqAsync)
            .WithDisplayName("Create Faq")
            .WithDescription("Create Faq , you can create new faq or create update your faq ")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer")
                .RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageFaq))
            .HasApiVersion(1.0);

        group.MapPut("", UpdateFaqAsync)
            .WithDisplayName("Update Faq")
            .WithDescription("Update Faq , you can create new faq or create update your faq ")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer")
                .RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageFaq))
            .HasApiVersion(1.0);

        group.MapDelete("{id}", DeleteFaqAsync)
            .WithDisplayName("Delete Faq")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageFaq))
            .HasApiVersion(1.0);
    }
    private async Task<IResult> DeleteFaqAsync([FromRoute]Guid id,[FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new DeleteFaqCommand(Id:id), cancellationToken));

    private async Task<IResult> GetFaqsAsync([FromQuery]int page, [FromQuery] int? count ,[FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetFaqsQuery(Count: count ?? 0 , Page:page), cancellationToken));

    private async Task<IResult> CreateFaqAsync([FromBody] CreateFaqCommand request,[FromServices]IMediator mediator , CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(request, cancellationToken));

    private async Task<IResult> UpdateFaqAsync([FromBody] UpdateFaqCommand request, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(request, cancellationToken));

    private async Task<IResult> GetFaqBySlugAsync([FromQuery] string slug, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetFaqQuery(null, slug), cancellationToken));

}