<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Http\UploadedFile;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class RoadItemsProject extends Model
{
    use HasFactory;
    ///start_way_id
    ///end_way_id

    protected $fillable = [
        'id',
        'user_id',
        'start_lat',
        'start_lng',
        'end_lat',
        'end_lng',
        'project_distance',
        'item',
        'item_fa',
        'sub_item',
        'sub_item_fa',
        'sub_item_data',
        'sub_items',
        'sub_items_data',
        'unit_fa',
        'created_at',
        'updated_at',
        'created_at_fa',
        'province_id',
        'province_fa',
        'city_id',
        'city_fa',
        'user_name',
        'is_new',
        'parent_id ',
        'start_way_id',
        'end_way_id',
        'info_id',
        'status',
        'status_fa',
        'supervisor_id',
        'supervisor_description',
        'supervising_time',
        'edarat_id',
        'edarat_name',
        'edarat_type',
        'supervisor_name',
        'activity_date',
        'activity_time',
        'activity_date_time',
    ];

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

    public function subitem()
    {
        return $this->belongsTo('App\Models\InfoItem', 'sub_item', 'sub_item');
    }

    public function user()
    {
        return $this->belongsTo('App\Models\User');
    }

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

    public function edarat()
    {
        return $this->morphTo();
    }

    public function observedItem()
    {
        return $this->hasOne(ObservedItem::class);
    }

    public static function store(User $user, array $attributes, UploadedFile $before_image = null, UploadedFile $after_image = null, int $observed_item_id = null)
    {
        try {
            $road_item = \DB::transaction(function () use ($user, $attributes, $before_image, $after_image, $observed_item_id) {
                $road_item = $user->roadItemsProjects()->create([
                    'start_lat' => $attributes['start_lat'],
                    'start_lng' => $attributes['start_lng'],
                    'end_lat' => $attributes['end_lat'],
                    'end_lng' => $attributes['end_lng'],
                    // 'project_distance' => $attributes['project_distance'],
                    'item' => $attributes['item'],
                    'item_fa' => $attributes['item_fa'],
                    'sub_item' => $attributes['sub_item'],
                    'sub_item_fa' => $attributes['sub_item_fa'],
                    'sub_item_data' => $attributes['sub_item_data'],
                    'province_id' => $attributes['province_id'],
                    'province_fa' => $attributes['province_fa'],
                    'city_id' => $attributes['city_id'],
                    'city_fa' => $attributes['city_fa'],
                    'user_name' => $attributes['user_name'],
                    'start_way_id' => $attributes['start_way_id'],
                    'end_way_id' => $attributes['end_way_id'],
                    'unit_fa' => $attributes['unit_fa'],
                    'created_at_fa' => $attributes['created_at_fa'],
                    'info_id' => $attributes['info_id'],
                    'status' => $attributes['status'],
                    'status_fa' => $attributes['status_fa'],
                    'edarat_id' => $attributes['edarat_id'],
                    'edarat_name' => $attributes['edarat_name'],
                    'edarat_type' => $attributes['edarat_type'],
                    'activity_date' => $attributes['activity_date'],
                    'activity_time' => $attributes['activity_time'],
                    'activity_date_time' => $attributes['activity_date']." ".$attributes['activity_time'],
                    'is_new' => 1,
                ]);

                // if ($info_item->needs_image) {
                if ($after_image) {
                    $after = $after_image->store('road_items_projects_new/after', 'public');
                    $road_item->files()->create(['path' => $after]);
                    $urlAfter = "/var/www/rms/public/storage/{$after}";
                    // \App\Helpers\Compress::resize($urlAfter);
                }
                if ($before_image) {
                    $before = $before_image->store('/road_items_projects_new/before', 'public');
                    $road_item->files()->create(['path' => $before]);
                    $urlBefore = "/var/www/rms/public/storage/{$before}";
                    // \App\Helpers\Compress::resize($urlBefore);
                }
                // }

                // if has observed item save project
                if ($observed_item_id) {
                    if (ObservedItem::where('id', $observed_item_id)->whereNotNull('road_item_id')->exists()) {
                        return -1;
                    }

                    ObservedItem::where('id', $observed_item_id)
                                ->update([
                                    'road_item_id' => $road_item->id
                                ]);
                }

                return $road_item;
            });

            return $road_item;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    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',
        );
    }
}
