<?php

namespace App\Policies;

use App\Models\Entry;
use App\Auth\XenForoUser as User;
use Illuminate\Auth\Access\Response;

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

        return false;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

            return false;

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

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

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

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

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

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

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

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

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

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

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

            return false;

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

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

            return false;

        }

        return false;
    }

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

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

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

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

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

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

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

}
