<?php

namespace App\Policies;

use App\Facades\Permissions;
use App\Models\User;
use App\Models\Vrf;

class VrfPolicy
{
    use ChecksGlobalPermissions;

    public function __construct()
    {
        $this->globalPrefix = 'routing';
    }

    /**
     * Determine whether the user can view any models.
     */
    public function viewAny(User $user): bool
    {
        return $this->hasGlobalPermission($user, 'view', true)
            || $this->hasGlobalPermission($user, 'viewAll')
            || $this->hasGlobalPermission($user, 'update');
    }

    /**
     * Determine whether the user can view all models.
     */
    public function viewAll(User $user): bool
    {
        return $this->hasGlobalPermission($user, 'viewAll');
    }

    /**
     * Determine whether the user can view the model.
     */
    public function view(User $user, Vrf $vrf): bool
    {
        if ($this->hasGlobalPermission($user, 'viewAll')) {
            return true;
        }

        return $this->hasGlobalPermission($user, 'view', true)
            && Permissions::canAccessDevice($vrf->device_id, $user);
    }

    /**
     * Determine whether the user can update the model.
     */
    public function update(User $user, Vrf $vrf): bool
    {
        return $this->hasGlobalPermission($user, 'update')
            && Permissions::canAccessDevice($vrf->device_id, $user);
    }
}
