<?php

namespace App\Models;

use Cviebrock\EloquentSluggable\Sluggable;
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\Model;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    use Sluggable;
    use SluggableScopeHelpers;
    use HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'name_ar',
        'name_en',
        'email',
        'password',
        'pio_ar',
        'pio_en',
        'password_show',
        'role_id',
        'photo',
        'phone',
        'is_active',
        'job_title_ar',
        'job_title_en',
        'is_notifiy',
    ];


    // get name translation
    public function getNameAttribute()
    {
        return $this->{'name_'.app()->getLocale()};
    } // end getNameAttribute

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

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

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];



    // Relation with role
    public function role()
    {
        return $this->belongsTo(Role::class);
    }


    // User is Admin?
    public function is_admin()
    {
        if ($this->hasRole('admin') && $this->is_active == 1) {
            return true;
        }

        return false;
    }


    public function posts()
    {
        return $this->hasMany(Post::class);
    }

    public function features()
    {
        return $this->hasMany(Feature::class);
    }

    public function company_schedules()
    {
        return $this->hasMany(CompanySchedule::class);
    }

    public function manufacturing_stages()
    {
        return $this->hasMany(ManufacturingStage::class);
    }

    public function manufacturing_companies()
    {
        return $this->hasMany(ManufacturingCompany::class);
    }

    /**
     * Return the sluggable configuration array for this model.
     *
     * @return array
     */
    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'name',
                'onUpdate' => true,
            ]
        ];
    }
}
