<?php

namespace App\Serp\Providers\DataForSeo;

use App\Serp\Data\SerpQuery;
use App\Serp\Exceptions\ProviderConfigurationException;

final class DataForSeoPayloadMapper
{
    private const LOCATIONS = ['TR' => 2792, 'DE' => 2276, 'US' => 2840, 'GB' => 2826];

    private const LANGUAGES = ['tr' => 'tr', 'de' => 'de', 'en' => 'en'];

    private const DEVICES = ['desktop' => 'windows', 'mobile' => 'android'];

    public function supports(SerpQuery $q): bool
    {
        return $q->searchEngine === 'google' && isset(self::LOCATIONS[strtoupper($q->countryCode)],self::LANGUAGES[strtolower($q->languageCode)],self::DEVICES[$q->device]);
    }

    public function map(SerpQuery $q): array
    {
        if (! $this->supports($q)) {
            throw new ProviderConfigurationException('Unsupported DataForSEO target.');
        }

        return ['keyword' => $q->phrase, 'location_code' => self::LOCATIONS[strtoupper($q->countryCode)], 'language_code' => self::LANGUAGES[strtolower($q->languageCode)], 'device' => $q->device, 'os' => self::DEVICES[$q->device], 'depth' => 10];
    }
}
