<?php

namespace App\Models;

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

class LiveChatMessage extends Model
{
    use HasFactory;

    protected $table = 'live_chat_messages';

    protected $fillable = [
        'chat_id',
        'sender_type', // visitor, admin, system
        'sender_id',
        'message',
        'has_been_read'
    ];

    protected $casts = [
        'has_been_read' => 'boolean',
    ];

    public function chat()
    {
        return $this->belongsTo(LiveChat::class, 'chat_id');
    }

    public function sender()
    {
        if ($this->sender_type === 'admin') {
            return $this->belongsTo(User::class, 'sender_id');
        } else if ($this->sender_type === 'visitor') {
            return $this->belongsTo(Visitor::class, 'sender_id');
        } else {
            // For 'system' or any other sender_type, return a null relation
            // But ensure it's a relation object, not null
            return $this->morphTo()->where('id', '<', 0); // This ensures an empty relation that won't match anything
        }
    }
} 