<?php

namespace Litecms\Content\Policies;

use Litepie\User\Contracts\UserPolicy;
use Litecms\Content\Models\Content;

class ContentPolicy
{

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

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

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

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

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

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

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

        return false;
    }

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