<?php namespace {{namespace}};

use Illuminate\Contracts\Auth\Guard;
use {{appnamespace}}\Http\Requests\CommentRequest;
use jorenvanhocht\Blogify\Models\Comment;
use jorenvanhocht\Blogify\Models\Post;

class CommentsController extends Controller
{

    /**
     * @var Comment
     */
    protected $comment;

    /**
     * @var mixed
     */
    protected $config;

    /**
     * @var Post
     */
    protected $post;

    /**
     * @var Guard
     */
    protected $auth;

    /**
     * @param Comment $comment
     * @param Post $post
     * @param Guard $auth
     */
    public function __construct(Comment $comment, Post $post, Guard $auth)
    {
        $this->comment = $comment;
        $this->config = objectify(config('blogify'));
        $this->post = $post;
        $this->auth = $auth;
    }


    public function store(CommentRequest $request)
    {
        $comment = new Comment;
        $comment->hash = blogify()->makeHash('comments', 'hash', true);
        $comment->content = $request->comment;
        $comment->user_id = $this->auth->user()->id;
        $comment->post_id = $this->post->byHash($request->post)->id;
        $comment->revised = ($this->config->approve_comments_first) ? 1 : 2;
        $comment->save();

        tracert()->log('comments', $comment->id, $this->auth->user()->id, 'create');

        session()->flash('notify', [ 'success', 'Your comment has been added' ] );

        return back();
    }

}