<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * @mixin IdeHelperConversation
 */
class Conversation extends Model
{
    protected $fillable = [
        'listing_id',
        'buyer_id',
        'consultant_profile_id',
        'last_message_at'
    ];

    public function listing()
    {
        // Soft-delete edilen ilanın sohbet geçmişi kaybolmasın (başlık vs. görünsün).
        return $this->belongsTo(Listing::class)->withTrashed();
    }

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

    public function messages()
    {
        return $this->hasMany(Message::class);
    }

    public function consultantProfile()
    {
        return $this->belongsTo(ConsultantProfile::class);
    }

    public function latestMessage()
    {
        return $this->hasOne(Message::class)->latestOfMany();
    }
}
