<?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', 'activation','email', 'password' , 'phone', 'photo','cost','role','commerical_log','commerical_photo','provider_name','latlng','start_time','end_time','facebook','twitter','instagram','role','city_id','country_id','user_id','category_id','whatsapp'
    ];

    /**
     * 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 description() :hasOne
    {
        return $this->hasOne(UserDescription::class,'user_id');
    }

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

    public function rates() :hasMany
    {
        return $this->hasMany(Rate::class,'provider_id')->orderBy('id','DESC');
    }
    public function photos() :hasMany
    {
        return $this->hasMany(UserPhoto::class);
    }
    public function events() :hasMany
    {
        return $this->hasMany(Product::class)->orderBy('id','DESC');
    }
    public function event()
    {
        $event = Product::where('user_id',$this->id)->where('date','>=',date('Y-m-d'))->first();
        return $event;
    }
    /**
     * 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;
    }
    public function rated()
    {
        $rate = Rate::where('user_id',\Auth::user()->id)->where('provider_id',$this->id)->first();
        return $rate ? false : true;
    }
    public function getRateAttribute()
    {
        $rate = (new RateController())->getRate($this);
        return $rate['rate'];
    }
    public function getRateCountAttribute()
    {
        $rate = (new RateController())->getRate($this);
        return $rate['count'];
    }
    /**
     * 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 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 static function withDescription($user_id = null)
    {
        $query = self::join('user_descriptions as ud', 'ud.user_id', 'users.id')
            ->where('ud.language_id', currentLanguage()->id)
            ->select(['users.created_at', 'ud.*']);

        if ($user_id)
        {
            if (is_array($user_id))
            {
                $query->whereIn('users.id', $user_id);
            }
            else
            {
                $query->where('users.id', $user_id);
            }
        }
        $query->select([
            'users.*',
            'ud.name',
            'ud.description',
        ]);
        return $query;
    }
}
