<?php

namespace Sahbaj\Sahbaj\Policies;

use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Sahbaj\Sahbaj\Models\Sahbaj;

class SahbajPolicy
{
    use HandlesAuthorization;

    /**
     * Determine if the given user can view the sahbaj.
     *
     * @param User $user
     * @param Sahbaj $sahbaj
     *
     * @return bool
     */
    public function view(User $user, Sahbaj $sahbaj)
    {
        if ($user->canDo('sahbaj.sahbaj.view') && $user->is('admin')) {
            return true;
        }

        return $user->id === $sahbaj->user_id;
    }

    /**
     * Determine if the given user can create a sahbaj.
     *
     * @param User $user
     * @param Sahbaj $sahbaj
     *
     * @return bool
     */
    public function create(User $user)
    {
        return  $user->canDo('sahbaj.sahbaj.create');
    }

    /**
     * Determine if the given user can update the given sahbaj.
     *
     * @param User $user
     * @param Sahbaj $sahbaj
     *
     * @return bool
     */
    public function update(User $user, Sahbaj $sahbaj)
    {
        if ($user->canDo('sahbaj.sahbaj.update') && $user->is('admin')) {
            return true;
        }

        return $user->id === $sahbaj->user_id;
    }

    /**
     * Determine if the given user can delete the given sahbaj.
     *
     * @param User $user
     * @param Sahbaj $sahbaj
     *
     * @return bool
     */
    public function destroy(User $user, Sahbaj $sahbaj)
    {
        if ($user->canDo('sahbaj.sahbaj.delete') && $user->is('admin')) {
            return true;
        }

        return $user->id === $sahbaj->user_id;
    }

    /**
     * Determine if the user can perform a given action ve.
     *
     * @param [type] $user    [description]
     * @param [type] $ability [description]
     *
     * @return [type] [description]
     */
    public function before($user, $ability)
    {
        if ($user->isSuperUser()) {
            return true;
        }
    }
}