namespace NetinaShop.Api.Controller;

public class ScraperController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("Scraper")
            .MapGroup("api/scraper")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());

        group.MapGet("digi", GetDigiProductsAsync)
            .WithDisplayName("GetDigiProducts")
            .HasApiVersion(1.0);

        group.MapPost("digi/{productId}", AddProductToShopAsync)
            .WithDisplayName("AddProductToShop")
            .HasApiVersion(1.0);
    }

    public async Task<IResult> GetDigiProductsAsync([FromQuery] string productName, [FromServices] IDigikalaScraper digikalaScraper, CancellationToken cancellationToken)
        => TypedResults.Ok(await digikalaScraper.GetProductsByNameAsync(productName));

    public async Task<IResult> AddProductToShopAsync(string productId, [FromQuery] string productName, [FromServices] IDigikalaScraper digikalaScraper, CancellationToken cancellationToken)
        => TypedResults.Ok(await digikalaScraper.AddProductToShopAsync(productId, productName,cancellationToken));
}