<?php

namespace App\Helpers\FirstParty\DFS;

use Exception;
use Http;

class DFSSerp
{
    public static function liveAdvanced($se, $se_type, $keyword, $location_name, $language_code, $depth, $search_param = null)
    {
        $api_url = config('dataforseo.url');

        $api_version = config('dataforseo.api_version');

        $api_timeout = config('dataforseo.timeout');

        $query = [
            'keyword' => $keyword,
            'location_name' => $location_name,
            'language_code' => $language_code,
        ];

        if (! is_empty($search_param)) {
            $query['search_param'] = $search_param;
        }

        try {
            $response = Http::timeout($api_timeout)->withBasicAuth(config('dataforseo.login'), config('dataforseo.password'))->withBody(
                json_encode([(object) $query])
            )->post("{$api_url}{$api_version}serp/{$se}/{$se_type}/live/advanced");

            if ($response->successful()) {
                return $response->body();
            }
        } catch (Exception $e) {
            return null;
        }

        return null;

    }
}
