<?php

namespace App\Models;

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

class LiveChat extends Model
{
    use HasFactory;

    protected $table = 'live_chats';

    protected $fillable = [
        'visitor_id',
        'status',
        'ip_address',
        'user_agent',
        'location',
        'page_url',
        'page_title',
        'referrer',
        'is_assigned',
        'assigned_to'
    ];

    public function visitor()
    {
        return $this->belongsTo(Visitor::class);
    }

    public function assignedTo()
    {
        return $this->belongsTo(User::class, 'assigned_to');
    }

    public function messages()
    {
        return $this->hasMany(LiveChatMessage::class, 'chat_id');
    }

    /**
     * Get the latest message for this chat
     */
    public function latestMessage()
    {
        return $this->hasOne(LiveChatMessage::class, 'chat_id')
            ->latest();
    }
} 