<?php
 
namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Auth;

class HeuristicsLevel implements Rule
{

    public function passes($attribute, $value)
    {
        if(!is_int($value)) {
            return false;
        }

        switch($value) {
            case 6: return Auth::hasUser() && Auth::user()->canDo([ 'goliath.heuristics.6' ]);
            case 5: return Auth::hasUser() && Auth::user()->canDo([ 'goliath.heuristics.5' ]);
            case 4: return Auth::hasUser() && Auth::user()->canDo([ 'goliath.heuristics.4' ]);
            case 3: return true;
            case 2: return true;
            case 1: return Auth::hasUser() && Auth::user()->canDo([ 'goliath.heuristics.1' ]);
            case 0: return Auth::hasUser() && Auth::user()->canDo([ 'goliath.heuristics.0' ]);
        }
    }
 
    public function message()
    {
        return 'The :attribute must be of a valid heuristics level available to the user.';
    }
}