<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Job extends Model
{

    protected $fillable = [
        "is_active",
    ];

    protected $casts = [
        "is_active" => "boolean",
    ];

    public function jobInfos()
    {
        return $this->hasMany(JobInfo::class)->with("jobInfoType")->orderBy("created_at");
    }

    /**
     * Get an associative collection of the job infos with their values
     * @return \Illuminate\Database\Eloquent\Collection<string, string>>
     */
    public function jobInfosTable() {
        return $this->jobInfos->mapWithKeys(function ($jobInfo) {
            return [$jobInfo->key => $jobInfo->value];
        });
    }

    public function jobRuns()
    {
        return $this->hasMany(JobRun::class)->orderBy("created_at");
    }
}
