<?php

namespace App\Models;

use Cviebrock\EloquentSluggable\Sluggable;
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    use HasFactory;
    use Sluggable;
    use SluggableScopeHelpers;

    protected $fillable = [
        'title_ar',
        'title_en',
        'desc_ar',
        'desc_en',
        'photo',
        'slug',
        'address',
        'completion_date',
        'customer_id',
        'company_id',
    ];

    protected $dates = ['completion_date'];

    public function keywords()
    {
        return $this->morphMany(KeyWord::class, 'keywordable');
    }

    // get name translation
    public function getTitleAttribute()
    {
        return $this->attributes['title_' . app()->getLocale()];
    } // end getNameAttribute

    // get desc translation
    public function getDescAttribute()
    {
        return $this->attributes['desc_' . app()->getLocale()];
    } // end getDescAttribute

    public function images()
    {
        return $this->morphMany(Image::class, 'imageable');
    }

    public function company()
    {
        return $this->belongsTo(ManufacturingCompany::class, 'company_id', 'id');
    }

    public function customer()
    {
        return $this->belongsTo(User::class, 'customer_id', 'id');
    }

    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'title_en',
                'onUpdate' => true,
            ]
        ];
    }
}
