<?php

namespace App\Models\Admin;

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

class Ticket extends Model
{
    use HasFactory;

    protected $table = "tickets";

    protected $fillable = [
        'user_id',
        'ticket_tip',
        'subject',
        'file',
    ];

    
    public function messages()
    {
        return $this->hasMany(TicketMessage::class)->orderBy('id','desc');
    }
    public function firstMessage()
    {
        return $this->hasOne(TicketMessage::class)->oldestOfMany();
    }
    public function lastMessageType()
    {
        return $this->hasOne(TicketMessage::class)->latestOfMany()->select('type');
    }
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
