<?php

namespace OtomaticAi\Utils;

use Exception;
use OtomaticAi\Api\OtomaticAi\Client;
use OtomaticAi\Vendors\Carbon\Carbon;
use OtomaticAi\Vendors\Illuminate\Support\Arr;

class GoogleNews
{
    static public function search(string $search, Language $language)
    {
        $items = [];

        try {
            $api = new Client;
            $results = $api->googleNewsSearch($search, $language->key);
            $now = Carbon::now()->format("Y-m-d H:i:s");

            foreach (Arr::get($results, "organic_results", []) as $result) {
                // add to items
                $items[] =  [
                    "title" => Arr::get($result, "title"),
                    "url" => Arr::get($result, "link"),
                    "pub_date" => $now,
                ];
            }
        } catch (Exception $e) {
        }

        return $items;
    }
}
