<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Mtc\Filter\Contracts\IsFilter;
use App\Filter\FilterIndex;

class UpdateFilterIndexForRecord implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(private Model $model, private IsFilter $filter, private string $filter_type)
    {
        $this->onQueue('sync');
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(): void
    {
        if (tenancy()->initialized === false) {
            return;
        }
        FilterIndex::index($this->filter_type, $this->model, $this->filter);
    }
}
