<?php

namespace App\Models;
use App\Models\Erp\Mrcart;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

class User extends Authenticatable
{
    use Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id', 
        'name', 
        'contac', 
        'email', 
        'password',
        'phone',
        'nit',
        'modality',
        'business_name',
        'address',
        'city',
        'regime',
        'rut',
        'type',
        'city_id',
        'grupo_id',
        'status_field',
        'country'
    ];


    /**
     * 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',
    ];
    public function brands_(){
        
        $nit = $this->nit;
        return Mrcart::with([
            'detail',
            "detail.tercero",
            'detail.tercero.detail',
            'control'])
            ->whereHas("detail.tercero" , function($q) use($nit){
                $q->where('entidad', $nit);
            })
            ->get();  
    }
    
    public function stores()
    {
        return $this->hasMany('App\Models\Stores\StoresUser','id_user','id');

    }
    public function brands()
    {
        return $this->hasMany(UserBrands::class);
    }
    public function catalogs()
    {
        return $this->hasManyThrough('App\Catalog', 'App\Brand');
    }
    public function visuals()
    {
        return $this->hasManyThrough('App\Models\Visual\Visual');
    }
    public function user_detail()
    {
        return $this->belongsTo('App\Models\UserProvider','id','user_id');
    }
    public function city()
    {
        return $this->belongsTo('App\Models\City\City');
    }
    //query scope
    public function scopeRazonSocial($query, $razonSocial){
        if($razonSocial){
            return $query->where('business_name', 'ilike', "%$razonSocial%");
        }
    }

    public function scopeNombre($query, $nombre){
        if($nombre){
            return $query->where('name', 'ilike', "%$nombre%");
        }
    }

    public function scopeNit($query, $nit){
        if($nit){
            return $query->where('nit', 'ilike', "%$nit%");
        }
    }

    public function scopeTelefono($query, $telefono){
        if($telefono){
            return $query->where('phone', 'ilike', "%$telefono%");
        }
    }

    public function scopeEmail($query, $email){
        if($email){
            return $query->where('email', 'ilike', "%$email%");
        }
    }
    public function scopeEstado($query, $estado){
        if($estado){
            return $query->where('status_field', 'ilike', "%$estado%");
        }
    }

    
}
