using NetinaShop.Repository.Repositories.Entity.Abstracts;
using System.Reflection;
using System.Text.Json;

namespace NetinaShop.Api.Controller;

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());


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

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

        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());
    }
    public async Task<IResult> GetPagesAsync(Guid id, [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();
    }
}