<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Kreait\Firebase\Factory;


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

    protected $model;

    public function __construct($model)
    {
        $this->model = $model;
    }

    public function handle()
    {
        $data = [
            $this->model->id => $this->model->toArray(),
        ];

        // Enviar $data a Google (implementa tu lógica aquí)
        $this->sendToGoogle($data);
    }

    protected function sendToGoogle($data)
    {
        $factory = (new Factory)->withServiceAccount(storage_path('app/' . env('FIREBASE_CREDENTIALS')));
        $firestore = $factory->createFirestore();
        $database = $firestore->database();
        $document =$this->model->getTable();
        $database->collection('solidclinic-'.env("APP_ID"))->document($document)->collection($this->model->id)->document("data")->set($data);
    }
}
