<?php
namespace Root\Core\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

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

    protected array $data;
    protected string $databaseConnection;

    /**
     * ایجاد نمونه جدید از Job با داده‌ها و نام دیتابیس
     *
     * @param array $data
     * @param string $databaseConnection
     */
    
    public function __construct(array $data, string $databaseConnection)
    {
        $this->data = $data;
        $this->databaseConnection = $databaseConnection;
    }

    /**
     * پردازش لاگ و ذخیره آن در دیتابیس مشخص‌شده
     *
     * @return void
     */
    public function handle(): void
    {

        DB::connection($this->databaseConnection)->table('logs')->insert([
            'id' => Str::uuid()->toString(),
            'module' => $this->data['module'],
            'userId' => $this->data['userId'],
            'targetId' => $this->data['targetId'],
            'key' => $this->data['key'],
            'createDateTime' => time(),
        ]);

    }
}
