<?php

namespace App\Models;

use App\Services\Rate\RateController;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;


/**
 * @property mixed role
 * @property mixed modules
 */
class User extends Authenticatable
{
    use Notifiable;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name'	,
        'email'	,
        'phone'	,
        'date_of_birth'	,
        'photo'	,
        'gender'	,
        'website_url'	,
        'certificate_commercial'	,
        'tax_certificate'	,
        'company_exhibition'	,
        'company_showroom'	,
        'verification_pdf'	,
        'amazon_url'	,
        'noon_url'	,
        'commission',
        'active_account'	,
        'password'	,
        'role'	,
        'currency_id'	,
        'country_id'	,
        'city_id'
    ];

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

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

    /**
     * Get the modules of this user
     * @return BelongsToMany
     */
    public function modules() :BelongsToMany
    {
        return $this->belongsToMany(Module::class,'user_modules');
    }

    public function descriptions($language_id= null)
    {
        if ($language_id) {
            $data = $this->hasMany(UserDescription::class)->where('language_id', $language_id)->first();
            if(!$data){
                $data = $this->hasMany(UserDescription::class)->first();
            }
            return $data;
        }
        $data = $this->hasMany(UserDescription::class)->where('language_id', currentLanguage()['id'])->first();
        return $data;
    }

    public function photos() :hasMany
    {
        return $this->hasMany(UserPhoto::class);
    }
    public function country()
    {
        return $this->belongsTo(Country::class)->withTrashed();
    }

    public function getbirthAttribute()
    {
        $date = date('Y ',strtotime($this->date_of_birth));
        $date .= __('web.'.date('F',strtotime($this->date_of_birth)));
        $date .= date(' d ',strtotime($this->date_of_birth));
        return $date;
    }

    public function city()
    {
        return $this->belongsTo(City::class)->withTrashed();
    }

    /**
     * Check if the user can access a module
     * @param string, module name
     * @return bool
     */
    public function hasModule($module) :bool
    {
        if($this->modules->contains('path',$module))
        {
            return true;
        }
        return false;
    }

    /**
     * Check if the user can access a module
     * @param string, module name
     * @return bool
     */
    public function canAccess($module)
    {
        if ($this->role == 'super_admin') {
            return true;
        } else {
            return $this->hasModule($module);
        }
        return false;
    }

    public static function withDescription($user_id = null)
    {
        $query = self::orderByDesc('users.id')->join('user_descriptions as sd', 'sd.user_id', 'users.id')
            ->where('sd.language_id', currentLanguage()->id)
            ->select(['users.created_at', 'sd.*']);
        if ($user_id){
            $query->where('users.id', $user_id);
        }

        $query->select([
            'users.*',
            'sd.name',
            'sd.purchasing',
        ]);
        return $query;
    }
}
