<?php

namespace App\Models;

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

class Mission extends Model
{
    use HasFactory;

    protected $guarded = [];

    protected function requestedMachines(): Attribute
    {
        return Attribute::make(
            get: fn($value) => json_decode($value)
        );
    }

    protected function area(): Attribute
    {
        return Attribute::make(
            get: fn($value) => json_decode($value)
        );
    }

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

    public function roadItems(): MorphToMany
    {
        return $this->morphedByMany(RoadItemsProject::class, 'missionable');
    }

    public function roadPatrols(): MorphToMany
    {
        return $this->morphedByMany(RoadPatrol::class, 'missionable');
    }

    public function roadObserveds(): MorphToMany
    {
        return $this->morphedByMany(RoadObserved::class, 'missionable');
    }

    public function machines(): 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 road_patrol():HasMany
    {
        return $this->hasMany(RoadPatrol::class);
    }
}
