<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Cache;


class FetchOperatingHours implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     */
    protected $restaurantName;
    protected $googlePlaceApiKey;

    public function __construct($restaurantName, $googlePlaceApiKey)
    {
        $this->restaurantName = $restaurantName;
        $this->googlePlaceApiKey = $googlePlaceApiKey;
    }

    public function handle()
    {
        // $client = new Client();
        // $url = 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json';

        // $response = $client->get($url, [
        //     'query' => [
        //         'fields' => 'place_id',
        //         'input' => $this->restaurantName,
        //         'inputtype' => 'textquery',
        //         'key' => $this->googlePlaceApiKey
        //     ]
        // ]);
        // $placeData = json_decode($response->getBody(), true);

        // if (isset($placeData['candidates'][0]['place_id'])) {
        //     $placeId = $placeData['candidates'][0]['place_id'];

        //     $url = 'https://maps.googleapis.com/maps/api/place/details/json';
        //     $response = $client->get($url, [
        //         'query' => [
        //             'fields' => 'opening_hours',
        //             'place_id' => $placeId,
        //             'key' => $this->googlePlaceApiKey
        //         ]
        //     ]);

        //     $data = json_decode($response->getBody(), true);
        //     if (isset($data['result']['opening_hours']['weekday_text'])) {
        //         $hours = $data['result']['opening_hours']['weekday_text'];
        //         Cache::put('restaurant_hours_' . $this->restaurantName, $hours, now()->addHours(24));
        //     } else {
        //         Cache::put('restaurant_hours_' . $this->restaurantName, "N/A", now()->addHours(24));
        //     }
        // } else {
        //     Cache::put('restaurant_hours_' . $this->restaurantName, "N/A", now()->addHours(24));
        // }
    }
}
