<?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\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SyncModelJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
    public $table, $id, $action;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($table, $id, $action) {
        $this->table = $table;
        $this->id = $id;
        $this->action = $action;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // Fetch the model data
        $model = DB::table($this->table)->find($this->id);

        // Send to Server B
        $res = Http::post('https://ge-as.support/api/sync', [
            'table' => $this->table,
            'action' => $this->action,
            'data' => $model,
        ]);
    }
}