<?php

namespace App\Domain\Support\Models;

use App\Domain\Identity\Models\User;
use App\Support\Concerns\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use RuntimeException;

/** One message on a ticket thread (§59). */
class SupportTicketMessage extends Model
{
    use HasUuid;

    protected $guarded = ['id', 'uuid'];

    protected function casts(): array
    {
        return ['is_internal' => 'boolean'];
    }

    protected static function booted(): void
    {
        // A conversation is a record of what was said to whom.
        static::updating(fn () => throw new RuntimeException('A ticket message cannot be edited once sent.'));
        static::deleting(fn () => throw new RuntimeException('Ticket messages cannot be deleted.'));
    }

    public function ticket(): BelongsTo
    {
        return $this->belongsTo(SupportTicket::class, 'support_ticket_id');
    }

    public function author(): BelongsTo
    {
        return $this->belongsTo(User::class, 'author_id');
    }

    public function isFromApplicant(): bool
    {
        return $this->author_side === 'applicant';
    }
}
