<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Domain\Feeds\Models\Feed;
use Illuminate\Pagination\Cursor;
use App\Notifications\CommentAdded;
use App\Domain\Comments\Models\Comment;
use Illuminate\Http\Request as HttpRequest;

class CommentController extends Controller
{

    public function __construct()
    {
        // $this->middleware('subs.paid');
    }

    public function show($feed_id)
    {
        $count_pagination = 50;
        $cursor_request = request('cursor') ?? null;
        $feed = Feed::find($feed_id);
        if(empty($feed)){
            return [
                'nextCursor' => null,
                'items' => []
            ];
        }
        $comments = $feed->comments()
            ->where('parent_id', null)
            ->withCount('children')
            ->with('user:id,first_name,last_name,username,photo_path,color,user_char')->cursorPaginate($count_pagination, ['*'], 'cursor', Cursor::fromEncoded($cursor_request));
        $nextCursor = $comments->nextCursor();
        if($nextCursor){
            $nextCursor = $nextCursor->encode();
        }else{
            $nextCursor = null;
        }
        return [
            'nextCursor' => $nextCursor,
            'items' => $comments->transform(function ($item) {
                $item->childrens = [];
                $item->closed_children_comments = 1;
                $item->created_at_humans = $item->created_at->diffForHumans();
                return $item;
            })
        ];
    }

    public function children($comment_id)
    {
        $count_pagination = 50;
        $cursor_request = request('cursor') ?? null;
        $comments = Comment::findOrFail($comment_id)
            ->children()
            ->with('user:id,first_name,last_name,username,photo_path,color,user_char', 'answer_to:id,username')
            ->cursorPaginate($count_pagination, ['*'], 'cursor', Cursor::fromEncoded($cursor_request));

        $nextCursor = $comments->nextCursor();
        if($nextCursor){
            $nextCursor = $nextCursor->encode();
        }else{
            $nextCursor = null;
        }

        return [
            'nextCursor' => $nextCursor,
            'items' => $comments->transform(function ($item) {
                $item->created_at_humans = $item->created_at->diffForHumans();
                return $item;
            })
        ];
    }

    public function store(HttpRequest $request, $feed_id)
    {
        $disabled_notify_main = 0;
        $feed = Feed::findOrFail($feed_id);

        $body = $request->input('body');
        $body = preg_replace('/(?<=\s|^)@\S+/', "", $body);
        $body = htmlentities(trim($body));

        $to = $request->input('to');
        $parent = $request->input('parent');

        $comment = new Comment;
        $comment->body = $body;
        $comment->user_id = auth()->user()->id;
        $comment->feed_id = $feed_id;
        if($parent){
            $comment->parent_id = $parent;
        }
        if($to){
            $comment->to_user_id = $to;
        }

        $feed->comments()->save($comment);
        $comment->setRelation('user', auth()->user());
        if($to){
            $toUser = User::find($to);
            $comment->setRelation('answer_to', $toUser);
            if($toUser->id !== auth()->user()->id){
                $disabled_notify_main = 1;
                $message = [
                    'user_id' => auth()->user()->id,
                    'node_id' => $feed_id,
                    'comment' => $body
                ];
                $toUser->notify(new CommentAdded($message));
            }
        }

        $comment->created_at_humans = $comment->created_at->diffForHumans();

        if(empty($parent)){
            $comment->childrens = [];
            $comment->closed_children_comments = 1;
        }

        if($feed->user_id !== auth()->user()->id && $disabled_notify_main === 0){
            $message = [
                'user_id' => auth()->user()->id,
                'node_id' => $feed_id,
                'comment' => $body
            ];
            $feed->user->notify(new CommentAdded($message));
        }

        return $comment;
    }

    public function remove($comment_id)
    {
        $remove_count = 1;
        $comment = Comment::findOrFail($comment_id);
        $children_count = $comment->children()->count();
        if($children_count){
            $comment->children()->forceDelete();
            $remove_count = $remove_count + $children_count;
        }
        $comment->forceDelete();

        // Comment::destroy($comment_id);
        return $remove_count;
    }


}
