<?php

namespace LaraBB\Forum\Models;

use App\Traits\Uuid;
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 LaraBB\Category\Models\Category;
use LaraBB\Post\Models\Post;
use LaraBB\Settings\Models\Prefix;
use LaraBB\Thread\Models\Thread;
use LaraBB\User\Models\User;

/**
 * Class Forum
 *
 * @property int $id
 * @property string $uuid
 * @property string $category_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 $title
 * @property string $description
 * @property string $slug
 * @property integer $thread_count
 * @property integer $posting_count
 * @property Category $category
 * @property Collection $permissions
 * @property Collection $users
 * @property Collection $threads
 * @property Post $lastpost
 * @property Collection $prefixes
 *
 * @package LaraBB\Forum\Models
 */
class Forum extends Model
{
    use Uuid;

    /**
     * @var string
     */
    protected $table = 'forums';
    /**
     * @var string
     */
    protected $primaryKey = 'uuid';

    /**
     * @return BelongsTo
     */
    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class, 'category_uuid', 'uuid');
    }

    /**
     * @return HasMany
     */
    public function permissions(): HasMany
    {
        return $this->hasMany(Permission::class, 'forum_uuid', 'uuid');
    }

    /**
     * @return BelongsToMany
     */
    public function users(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'forums_has_users', 'forum_uuid', 'user_uuid')->withTimestamps();
    }

    /**
     * @return HasMany
     */
    public function threads(): HasMany
    {
        return $this->hasMany(Thread::class, 'forum_uuid', 'uuid');
    }

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

    /**
     * @return BelongsToMany
     */
    public function prefixes(): BelongsToMany
    {
        return $this->belongsToMany(Prefix::class, 'forums_has_prefixes', 'forum_uuid', 'prefix_uuid');
    }
}
