<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Casts\Attribute;

class RoadPatrol extends Model
{
    use HasFactory;

    protected $guarded = ['id'];

    protected function stopPoints(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => $value ? json_decode($value) : null,
        );
    }

    public function observedItems(): HasMany
    {
        return $this->hasMany(ObservedItem::class);
    }

    public function cmmsMachines(): MorphToMany
    {
        return $this->morphToMany(CMMSMachine::class,
            'machinable',
            'machinables',
            'machinable_id',
            'machine_id'
        );
    }

    public function rahdaran(): MorphToMany
    {
        return $this->morphToMany(Rahdaran::class,
            'rahdarable',
            'rahdarables',
            'rahdarable_id',
            'rahdar_id',
        );
    }

    public function missions(): MorphToMany
    {
        return $this->morphToMany(Mission::class,
            'missionable',
            'missionables',
            'missionable_id',
            'mission_id',
        );
    }
}
