<?php

namespace App\Models;

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

class SupportReply extends Model
{
    use HasFactory;

    protected $fillable = [
        'support_message_id',
        'user_id',
        'message',
        'is_agent_reply',
        'attachments',
        'read_at',
    ];

    protected $casts = [
        'is_agent_reply' => 'boolean',
        'attachments' => 'array',
        'read_at' => 'datetime',
    ];

    public function supportMessage()
    {
        return $this->belongsTo(SupportMessage::class);
    }

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

    public function scopeUnread($query)
    {
        return $query->whereNull('read_at');
    }

    public function scopeAgentReplies($query)
    {
        return $query->where('is_agent_reply', true);
    }

    public function scopeUserReplies($query)
    {
        return $query->where('is_agent_reply', false);
    }

    public function markAsRead()
    {
        $this->read_at = now();
        $this->save();
    }

    public function isFromAgent()
    {
        return $this->is_agent_reply;
    }
}
