<?php

namespace Fegerer\AppTest\Policies;

use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Fegerer\AppTest\Models\Action;

class ActionPolicy
{
    use HandlesAuthorization;

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

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

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

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

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

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

        return $user->id === $action->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;
        }
    }
}