<?php

namespace App\Models;

use App\Models\Concerns\BelongsToOwner;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;

class DirectMessage extends Model
{
    use BelongsToOwner;

    protected $table = 'meet_direct_messages';

    protected $fillable = [
        'uuid', 'owner_ref', 'conversation_id', 'sender_ref', 'sender_name', 'body', 'parent_id',
    ];

    protected static function booted(): void
    {
        static::creating(function (DirectMessage $message) {
            if (empty($message->uuid)) {
                $message->uuid = (string) Str::uuid();
            }
        });
    }

    public function conversation(): BelongsTo
    {
        return $this->belongsTo(DirectConversation::class, 'conversation_id');
    }

    public function parent(): BelongsTo
    {
        return $this->belongsTo(DirectMessage::class, 'parent_id');
    }

    public function replies(): HasMany
    {
        return $this->hasMany(DirectMessage::class, 'parent_id');
    }
}
