<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laracasts\Presenter\PresentableTrait;

class Post extends Model
{
    use PresentableTrait;

    protected $table = 'posts';

    protected $presenter = 'App\\Presenters\\PostPresenter';

    public function user()
    {
        return $this->belongsTo('App\\Models\\User', 'user_id');
    }

    public function page()
    {
        return $this->belongsTo('App\\Models\\Page', 'page_id');
    }

    public function toUser()
    {
        return $this->belongsTo('App\\Models\\User', 'to_user_id');
    }

    public function sharedUser()
    {
        return $this->belongsTo('App\\Models\\User', 'shared_from');
    }

    public function comments()
    {
        /*if(!\Auth::check() ||  (\Auth::check() and \Auth::user()->post_comment_admin==0)){
                $comments = $this->hasMany('App\\Models\\Comment', 'type_id')->with('user')->where('type', '=', 'post')->where('sensitive_comment', 0)->orderBy('id', 'desc');
        }else{
            $comments = $this->hasMany('App\\Models\\Comment', 'type_id')->with('user')->where('type', '=', 'post')->orderBy('id', 'desc');
        }*/

        // $comments = $this->hasMany('App\\Models\\Comment', 'type_id')->with('user')->where('type', '=', 'post')->orderBy('id', 'desc');

        $comments = $this->hasMany('App\\Models\\Comment', 'type_id')->with('user')->where('type', '=', 'post')->orderBy('id', 'desc');

        $bannedUsers = app('App\\Repositories\\UserRepository')->listBannedIds();

        $comments = $comments->whereNotIn('user_id', $bannedUsers);

        return $comments;
    }

    public function likes()
    {
        return $this->hasMany('App\\Models\\Like', 'type_id')->where('type', '=', 'post');
    }

    public function countLikes()
    {
        $postLikes = count($this->likes);
        $postCommentsLinkes = app('App\\Repositories\\LikeRepository')->getCommentsLikeByPost($this->id);

        // return app('App\\Repositories\\LikeRepository')->count('post', $this->id);
        return $postLikes + $postCommentsLinkes;
    }

    public function hasLiked()
    {
        if (! \Auth::check()) {
            return false;
        }

        return app('App\\Repositories\\LikeRepository')->hasLiked('post', $this->id, \Auth::user()->id);
    }

    public function getComments()
    {
        $comments = $this->comments()->with('user')->where('type', '=', 'post');

        /*if(!\Auth::check() ||  (\Auth::check() and \Auth::user()->post_comment_admin==0)){
            $comments = $comments->where('sensitive_comment', 0);
        }*/

        if (\Auth::check()) {
            $blockedUsers = app('App\\Repositories\\blockedUserRepository')->listIds(\Auth::user()->id);
            $comments = $comments->whereNotIn('user_id', $blockedUsers);
        }

        return $comments = $comments->orderBy('id', 'desc')->paginate(config('post-per-page'));
    }

    public function countComments()
    {
        // return $this->comments()->where('sensitive_comment', 0)->count();
        return $this->comments->count();
    }

    public function community()
    {
        return $this->belongsTo('App\\Models\\Community', 'community_id');
    }

    public function postedIncommunity()
    {
        return $this->belongsTo('App\\Models\\Community', 'posted_in_community');
    }

    public function communityCategory()
    {
        return $this->belongsTo('App\\Models\\CommunityCategory', 'type_id');
    }

    public function isOwner()
    {
        if (! \Auth::check()) {
            return false;
        }
        if (\Auth::user()->id == $this->user_id) {
            return true;
        }

        return false;
    }

    public function hasPosted()
    {
        return app('App\\Repositories\\ModeratorRepository')->hasPosted($this->id, \Auth::user()->id);
    }
}
