<?php

namespace Cleancrm\Advancedcrm\Policies;

use Litepie\User\Contracts\UserPolicy;
use Cleancrm\Advancedcrm\Models\RelationType;

class RelationTypePolicy
{

    /**
     * Determine if the given user can view the relation_type.
     *
     * @param UserPolicy $user
     * @param RelationType $relation_type
     *
     * @return bool
     */
    public function view(UserPolicy $user, RelationType $relation_type)
    {
        if ($user->canDo('advancedcrm.relation_type.view') && $user->isAdmin()) {
            return true;
        }

        return $relation_type->user_id == user_id() && $relation_type->user_type == user_type();
    }

    /**
     * Determine if the given user can create a relation_type.
     *
     * @param UserPolicy $user
     * @param RelationType $relation_type
     *
     * @return bool
     */
    public function create(UserPolicy $user)
    {
        return  $user->canDo('advancedcrm.relation_type.create');
    }

    /**
     * Determine if the given user can update the given relation_type.
     *
     * @param UserPolicy $user
     * @param RelationType $relation_type
     *
     * @return bool
     */
    public function update(UserPolicy $user, RelationType $relation_type)
    {
        if ($user->canDo('advancedcrm.relation_type.edit') && $user->isAdmin()) {
            return true;
        }

        return $relation_type->user_id == user_id() && $relation_type->user_type == user_type();
    }

    /**
     * Determine if the given user can delete the given relation_type.
     *
     * @param UserPolicy $user
     * @param RelationType $relation_type
     *
     * @return bool
     */
    public function destroy(UserPolicy $user, RelationType $relation_type)
    {
        return $relation_type->user_id == user_id() && $relation_type->user_type == user_type();
    }

    /**
     * Determine if the given user can verify the given relation_type.
     *
     * @param UserPolicy $user
     * @param RelationType $relation_type
     *
     * @return bool
     */
    public function verify(UserPolicy $user, RelationType $relation_type)
    {
        if ($user->canDo('advancedcrm.relation_type.verify')) {
            return true;
        }

        return false;
    }

    /**
     * Determine if the given user can approve the given relation_type.
     *
     * @param UserPolicy $user
     * @param RelationType $relation_type
     *
     * @return bool
     */
    public function approve(UserPolicy $user, RelationType $relation_type)
    {
        if ($user->canDo('advancedcrm.relation_type.approve')) {
            return true;
        }

        return false;
    }

    /**
     * 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;
        }
    }
}