<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


class ForumThread extends Model
{

    use SoftDeletes;

    /**
     * Don't auto-apply mass assignment protection.
     *
     * @var array
     */
    protected $guarded = [];

    // I like to explicitly define tables
    protected $table = 'forum_threads';

    // also fillable
    protected $fillable = [
        'user_id',
//        'forum_channels_id',
        'title',
        'body'
    ];

    // the thread path
    public function path()
    {
        return \url('/forum/threads/') . '/' . $this->id;
        //return "/threads/{$this->channel->slug}/{$this->id}";
    }

    // the user that created this thread
    public function user()
    {
        return $this->hasOne('App\Models\Auth\User', 'id', 'user_id');
    }

    // the replies to this thread
    public function replies()
    {
        return $this->hasMany('App\Models\ForumReply', 'thread_id', 'id');
    }

    // the channel(s)/topics this thread belongs to
    public function channels()
    {
        return $this->belongsToMany('App\Models\ForumChannel', 'forum_channels_forum_threads', 'forum_threads_id', 'forum_channels_id')->withPivot('forum_channels_id');
    }

    // add a reply. Should this be in the controller?
    public function addReply($forumReply)
    {
        $this->replies()->create($forumReply);
    }

}
