<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Plank\Mediable\Mediable;

class TicketMessage extends Model
{
    use HasFactory, Mediable;

    protected $guarded = ['id'];

    public static function boot()
    {
        parent::boot();

        static::deleting(function ($model) {
            if (!$model->hasMedia('attachments')) {
                return;
            }

            $model->getMedia('attachments')->each(function ($item, $key) {
                $item->delete();
            });
        });
    }

    public function ticket()
    {
        return $this->belongsTo(Ticket::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
