<?php

namespace App\Policies;

use App\Models\Entry;
use App\Models\News;
use App\Auth\XenForoUser as User;

class NewsPolicy
{
    public function viewAny(User $user): bool
    {
        if( $user->_can( 'romhackplaza', 'view' ) )
            return true;

        return false;
    }

    public function viewPending(User $user, News $news): bool
    {
        // Author.
        if( $news->user_id === $user->user_id )
            return true;

        return $user->_can( 'romhackplaza', 'canModerateEntries' );
    }

    public function viewDraft(User $user, News $news): bool    {
        // Author.
        if( $news->user_id === $user->user_id )
            return true;

        return $user->_can( 'romhackplaza', 'canSeeOthersDrafts' );
    }

    public function viewRejected(User $user, News $news): bool
    {
        // Author.
        if( $news->user_id === $user->user_id )
            return true;

        return $user->_can( 'romhackplaza', 'canSeeRejectedEntries' );
    }

    public function viewHidden(User $user, News $news): bool
    {
        return $user->_can('romhackplaza', 'canSeeHiddenEntries' );
    }

    public function viewLocked(User $user, News $news): bool
    {
        // Author.
        if( $news->user_id === $user->user_id )
            return true;

        return $user->_can('romhackplaza', 'canSeeLockedEntries' );
    }

    public function create(User $user, ?News $news = null ): bool
    {
        return $user->_can( 'romhackplaza', 'canSubmitEntry' );
    }

    /**
     * Determine whether the user can update the model.
     */
    public function update(User $user, News $news): bool
    {
        if( $news->state === 'published' ){

            // Staff editors
            if( $user->_can('romhackplaza', 'canEditOthersEntries') )
                return true;

            // Author.
            if( $user->_can( 'romhackplaza', 'canEditMyEntries' ) && $news->user_id === $user->user_id )
                return true;

            return false;

        } else if( $news->state === 'pending' ){

            // Staff moderation.
            if( $user->_can('romhackplaza', 'canEditOthersEntries') && $user->_can('romhackplaza', 'canModerateEntries') )
                return true;

            // Author.
            if( $user->_can( 'romhackplaza', 'canEditMyEntries' ) && $news->user_id === $user->user_id )
                return true;

        } else if( $news->state === 'draft' ){

            // Staff.
            if( $user->_can('romhackplaza', 'canEditOthersEntries') && $user->_can( 'romhackplaza', 'canSeeOthersDrafts' ) )
                return true;

            // Author.
            if( $user->_can( 'romhackplaza', 'canEditMyEntries' ) && $news->user_id === $user->user_id )
                return true;

        } else if( $news->state === 'rejected' ){

            // Staff.
            if( $user->_can('romhackplaza', 'canEditOthersEntries') && $user->_can( 'romhackplaza', 'canSeeRejectedEntries' ) )
                return true;

            // Author.
            if( $user->_can( 'romhackplaza', 'canEditMyEntries' ) && $news->user_id === $user->user_id )
                return true;

        } else if( $news->state === 'locked' ){

            // Staff.
            if( $user->_can('romhackplaza', 'canEditOthersEntries') && $user->_can( 'romhackplaza', 'canSeeLockedEntries' ) )
                return true;

            return false;

        } else if( $news->state === 'hidden' ){

            // Staff.
            if( $user->_can('romhackplaza', 'canEditOthersEntries') && $user->_can( 'romhackplaza', 'canSeeHiddenEntries' ) )
                return true;

            return false;

        }

        return false;
    }

    public function skipQueue(User $user, ?News $news = null): bool
    {
        return $user->_can( 'romhackplaza', 'canSubmitEntryInPublished' );
    }

    public function updateComment(User $user, News $news): bool
    {
        return $user->_can('romhackplaza', 'canModerateEntries' );
    }

    public function manageButtonsInQueue(User $user, News $news): bool
    {
        if( $news->state === 'rejected' ){
            return $this->viewRejected( $user, $news );
        }

        return $user->_can('romhackplaza', 'canModerateEntries' );
    }

    public function approve(User $user, News $news): bool
    {
        return $user->_can('romhackplaza', 'canModerateEntries' );
    }

    public function reject(User $user, News $news): bool
    {
        return $user->_can('romhackplaza', 'canModerateEntries' );
    }

    public function moderate(User $user, News $news): bool
    {
        return $user->_can('romhackplaza', 'canModerateEntries' );
    }
}
