<?php

namespace App\Serp\Providers\DataForSeo;

use App\Serp\Contracts\AsyncSerpProvider;
use App\Serp\Data\SerpQuery;
use App\Serp\Data\SerpResponse;
use App\Serp\Data\SerpSubmission;
use App\Serp\Data\SerpTaskResult;

final class DataForSeoProvider implements AsyncSerpProvider
{
    public function __construct(private DataForSeoClient $client, private DataForSeoPayloadMapper $mapper, private DataForSeoResponseNormalizer $normalizer) {}

    public function key(): string
    {
        return 'dataforseo';
    }

    public function supports(SerpQuery $q): bool
    {
        return $this->mapper->supports($q);
    }

    public function search(SerpQuery $q): SerpResponse
    {
        return $this->normalizer->live($this->client->live($this->mapper->map($q)), 10);
    }

    public function submit(SerpQuery $q): SerpSubmission
    {
        return $this->normalizer->submission($this->client->submit($this->mapper->map($q)));
    }

    public function collect(string $taskId, int $limit): SerpTaskResult
    {
        return $this->normalizer->collection($this->client->collect($taskId), $taskId, min(10, $limit));
    }
}
