<?php

namespace LaraBB\Post\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 LaraBB\Group\Models\Group;
use LaraBB\Thread\Models\Thread;
use LaraBB\User\Models\User;

/**
 * Class Post
 *
 * @property int $id
 * @property string $uuid
 * @property Carbon $created_at
 * @property string $created_uuid
 * @property User $createdBy
 * @property Carbon $updated_at
 * @property string $updated_uuid
 * @property User $updated_by
 * @property string $thread_uuid
 * @property string $title
 * @property string $content
 * @property Thread $thread
 * @property Collection $users
 * @property Collection $groups
 *
 * @package LaraBB\Post\Models
 */

class Post extends Model
{
    use Uuid;
    use Who;

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

    /**
     * @return BelongsTo
     */
    public function thread(): BelongsTo
    {
        return $this->belongsTo(Thread::class, 'thread_uuid', 'uuid');
    }

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

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

    /**
     * @return BelongsToMany
     */
    public function files(): BelongsToMany
    {
        return $this->belongsTomany(Userfile::class, 'posts_has_users_files', 'post_uuid', 'userfile_uuid');
    }
}
