<?php

namespace Modules\User\Models;

use App\Services\Upload\FileService;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Auth;
use Modules\Product\Models\Favorite;
use Modules\Store\Models\Store;
use Modules\User\Models\Module;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\User\Services\AccountService;


/**
 * @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',
        'photo',
        'password',
        'role',
        'store_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',
    ];

    public function store(): BelongsTo
    {
        return $this->belongsTo(Store::class)->withTrashed();
    }

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

    /**
     * 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 function favorited($product_id)
    {
        $account = new AccountService();
        $account = $account->getAccount();
        $favorite = $this->hasOne(Favorite::class)->where('status',$account['status'])->where('user_id',Auth::user()->id)->where('product_id',$product_id)->first();
        return $favorite;
    }

    public function setPhotoAttribute($value)
    {
        if($value)
        {
            if(is_string($value))
            {
                $this->attributes['photo'] = $value;
            }
            else
            {
                $photo = FileService::uploadImage($value);
                $this->attributes['photo'] = $photo;
            }
        }
        else
        {
            $this->attributes['photo'] = request()->method() == 'POST'  ? 'logo.png' : $this->photo;
        }
    }

}
