<?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\MorphToMany;
use Illuminate\Support\Facades\Storage;

class RoadObserved extends Model
{
    use HasFactory;

    protected $fillable = [
        'AutoID',
        'Title',
        'StartTime',
        'EndTime',
        'StartDate',
        'EndDate',
        'StartTime_DateTime',
        'EndTime_DateTime',
        'fk_RegisteredEventMessage',
        'fk_Country',
        'ProvinceName',
        'fk_Province',
        'fk_Town',
        'TownName',
        'lat',
        'lng',
        'XLAM',
        'YLAM',
        'WeatherName',
        'fk_Weather',
        'EventTypeTitle',
        'fk_EventType',
        'FeatureTypeTitle',
        'fk_FeatureType',
        'Description',
        'MobileForSendEventSms',
        'rms_province_id',
        'rms_city_id',
        'handling_way_id',
        'StartTime_DateTime_fa',
        'updated_at_fa',
        'updated_at',
        'observed_way_id',
        'rms_last_activity',
        'rms_last_activity_fa',
        'image_after',
        'image_before',
        // 'handler_type', 
        'edarate_shahri_id',
        'province_id',
        'city_id',
        'province_fa',
        'city_fa',
        'rms_status',
        'status',
        'status_fa',
        'supervisor_id',
        'supervisor_description',
        'supervising_time',
        'supervisor_name',
        'rms_start_latlng',
        'rms_end_latlng',
        'rms_description',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'rms_start_latlng' => 'array',
        'rms_end_latlng' => 'array'
    ];

    /**
     * Get all of the project's files.
     */
    public function files()
    {
        return $this->morphMany('App\Models\ProjectFile', 'project_fileable');
    }

    public function problemHistories()
    {
        return $this->hasMany('App\Models\RoadObservedHistory', 'problem_id');
    }

    public function province()
    {
        return $this->belongsTo('App\Models\Province', 'rms_province_id');
    }

    public function city()
    {
        return $this->belongsTo('App\Models\City', 'rms_city_id');
    }

    public function edarateShahri()
    {
        return $this->belongsTo('App\Models\EdarateShahri', 'edarate_shahri_id');
    }

    protected function imageAfter(): Attribute
    {
        return Attribute::make(
            get: fn($value) => $value == null ? null :
                (filter_var($value, FILTER_VALIDATE_URL)
                    ? $value
                    : Storage::disk('public')->url($value))
        );
    }

    protected function imageBefore(): Attribute
    {
        return Attribute::make(
            get: fn($value) => $value == null ? null :
                (filter_var($value, FILTER_VALIDATE_URL)
                    ? $value
                    : Storage::disk('public')->url($value))
        );
    }

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