<?php

namespace Litepie\Medium\Policies;

use Litepie\User\Contracts\UserPolicy;
use Litepie\Medium\Models\Variant;

class VariantPolicy
{

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

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

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

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

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

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

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

        return false;
    }

    /**
     * Determine if the given user can approve the given variant.
     *
     * @param UserPolicy $user
     * @param Variant $variant
     *
     * @return bool
     */
    public function approve(UserPolicy $user, Variant $variant)
    {
        if ($user->canDo('medium.variant.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;
        }
    }
}