<?php
 
namespace App\Rules;

use App\Models\Goliath\GoliathRuleset;
use App\Models\Goliath\RulesetType;
use Illuminate\Contracts\Validation\Rule;
use Exception;
use Illuminate\Support\Facades\Auth;

class RulesetIdentity implements Rule
{

    public function passes($attribute, $value)
    {

        try {

            return GoliathRuleset::where('id', '=', $value)
                ->where(function($q) { 
                    $q->where([['type', '=', RulesetType::Universal], ['organisation_id', '=', 1]])
                    ->orWhere('organisation_id', '=', Auth::user()->organisation_id);
                })
                ->count() > 0;

        }
        catch(Exception $ex) {
            return false;
        }

    }

    public function message()
    {
        return 'The :attribute must correlate to a ruleset that the user should be allowed to see.';
    }
}