<?php

namespace LaraBB\Thread\Models;

use App\Traits\Uuid;
use App\Traits\Who;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use LaraBB\Forum\Models\Forum;
use LaraBB\Group\Models\Group;
use LaraBB\Post\Models\Post;
use LaraBB\Settings\Models\Prefix;
use LaraBB\User\Models\User;

/**
 * Class Thread
 *
 * @property int $id
 * @property string $uuid
 * @property Carbon $created_at
 * @property string $created_uuid
 * @property Carbon $updated_at
 * @property string $updated_uuid
 * @property Carbon $lastpost_at
 * @property string $lastpost_uuid
 * @property string $firstpost_uuid
 * @property string $forum_uuid
 * @property string $prefix_uuid
 * @property string $title
 * @property string $slug
 * @property int $views
 * @property int $posts
 * @property Forum $forum
 * @property Collection $groups
 * @property Collection $users
 * @property Collection $postings
 * @property Post $lastpost
 * @property Prefix $prefix
 *
 * @package LaraBB\Thread\Models
 */
class Thread extends Model
{
    use Uuid;
    use Who;

    protected $table = 'threads';
    protected $primaryKey = 'uuid';

    /**
     * @return BelongsTo
     */
    public function forum(): BelongsTo
    {
        return $this->belongsTo(Forum::class, 'forum_uuid', 'uuid');
    }

    /**
     * @return BelongsToMany
     */
    public function groups(): BelongsToMany
    {
        return $this->belongsToMany(Group::class, 'threads_has_groups', 'thread_uuid', 'group_uuid');
    }

    /**
     * @return BelongsToMany
     */
    public function users(): BelongsToMany
    {
        return $this->belongsTomany(User::class, 'threads_has_users', 'thread_uuid', 'user_uuid');
    }

    /**
     * @return HasMany
     */
    public function postings(): HasMany
    {
        return $this->hasMany(Post::class, 'thread_uuid', 'uuid');
    }

    /**
     * @return BelongsTo
     */
    public function lastpost(): BelongsTo
    {
        return $this->belongsTo(Post::class, 'lastpost_uuid', 'uuid');
    }

    /**
     * @return HasOne
     */
    public function prefix(): HasOne
    {
        return $this->hasOne(Prefix::class, 'uuid', 'prefix_uuid');
    }
}
