namespace Netina.Api.Controllers;

public class PageController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("Pages")
            .MapGroup("api/page");

        group.MapGet("", GetPagesAsync)
            .WithDisplayName("Get Pages")
            .HasApiVersion(1.0)
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ViewPages, ApplicationPermission.ManagePages));


        group.MapGet("{id}", GetPageByIdAsync)
            .WithDisplayName("Get Page")
            .HasApiVersion(1.0)
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ViewPages, ApplicationPermission.ManagePages));

        group.MapGet("slug/{pageSlug}", GetPageAsync)
            .WithDisplayName("Get Page")
            .HasApiVersion(1.0)
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ViewPages, ApplicationPermission.ManagePages));

        group.MapGet("type/{type}", GetPageByTypeAsync)
            .WithDisplayName("Get Page")
            .HasApiVersion(1.0);

        group.MapPost("", PostPageAsync)
            .WithDisplayName("Post Page")
            .HasApiVersion(1.0)
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManagePages));


        group.MapDelete("{id}", DeletePageByIdAsync)
            .WithDisplayName("Delete Page")
            .HasApiVersion(1.0)
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ViewPages, ApplicationPermission.ManagePages));

    }

    private async Task<IResult> DeletePageByIdAsync([FromRoute]Guid id,[FromServices]IPageService pageService,CancellationToken cancellationToken)
        => TypedResults.Ok(await pageService.DeletePageAsync(id, cancellationToken));

    public async Task<IResult> GetPagesAsync([FromServices] IPageService pageService, CancellationToken cancellationToken)
    {
        return TypedResults.Ok(await pageService.GetPagesAsync(cancellationToken));
    }

    public async Task<IResult> GetPageByIdAsync(Guid id ,[FromServices] IPageService pageService, CancellationToken cancellationToken)
    {
        return TypedResults.Ok(await pageService.GetPageAsync(id: id,cancellationToken: cancellationToken));
    }
    public async Task<IResult> GetPageByTypeAsync(string type, [FromServices] IPageService pageService, CancellationToken cancellationToken)
    {
        return TypedResults.Ok(await pageService.GetPageAsync(type: type, cancellationToken: cancellationToken));
    }

    public async Task<IResult> GetPageAsync(string pageSlug, [FromServices] IPageService pageService, CancellationToken cancellationToken)
    {
        return TypedResults.Ok(await pageService.GetPageAsync(pageSlug: pageSlug,cancellationToken: cancellationToken));
    }

    public async Task<IResult> PostPageAsync([FromBody] PageActionRequestDto page, [FromServices] IPageService pageService, CancellationToken cancellationToken)
    {
        await pageService.CreatePageAsync(page, cancellationToken);
        return TypedResults.Ok();
    }
}