<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;

class Job extends Model
{
    public $timestamps = false;

    protected $casts = [
        'payload' => 'array',
        'reserved_at' => 'datetime',
        'available_at' => 'datetime',
        'created_at' => 'datetime',
    ];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->table = Config::get('queue.connections.' . (Config::get('queue.default', 'database')) . '.table', 'jobs');
    }

	public function getDisplayNameAttribute()
	{
		return $this->payload['displayName'];
	}

	public function getMaxTriesAttribute()
	{
		return $this->payload['maxTries'];
	}

	public function getDelayAttribute()
	{
		return $this->payload['delay'];
	}

	public function getUUIDAttribute()
	{
		return $this->payload['uuid'];
	}

	public function getTimeoutAttribute()
	{
		return $this->payload['timeout'];
	}

	public function getRetryUntilAttribute()
	{
		return !is_null($this->payload['retryUntil']) ? new \Carbon\Carbon($this->payload['retryUntil']) : null;
	}

	public function getTimeoutAtAttribute()
	{
		return !is_null($this->payload['timeout_at']) ? new \Carbon\Carbon($this->payload['timeout_at']) : null;
	}

	public function getCommandNameAttribute()
	{
		return $this->payload['data']['commandName'];
	}

	public function getCommandAttribute()
	{
		return unserialize($this->payload['data']['command']);
	}
}