<?php

namespace App\Traits;

use App\Jobs\SyncModelJob;
// use Illuminate\Support\Facades\Log;

trait GlobalModelEvents {
    protected function dispatchSyncJob($model, $action) {
        // if (in_array($model->getTable(), ['jobs', 'failed_jobs', 'migrations'])) {
        //     return;
        // }
    
        // SyncModelJob::dispatch($model->getTable(), $model->getKey(), $action)->onConnection('sync');
        
        return;
    }
    
    
    protected static function booted() {
        static::created(function ($model) {
            $instance = new self();
            $instance->dispatchSyncJob($model, 'created');
        });
        static::updated(function ($model) {
            $instance = new self();
            $instance->dispatchSyncJob($model, 'updated');
        });
        static::deleted(function ($model) {
            $instance = new self();
            $instance->dispatchSyncJob($model, 'deleted');
        });
    }
}