<?php

namespace App\Models;

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

class ChatSession extends Model
{
    use HasFactory;

    protected $fillable = [
        'visitor_token',
        'phone',
        'started_at',
        'last_activity_at',
        'active_seconds',
        'visit_count',
        'is_returning',
        'ip_address',
        'user_agent',
        'lead_status',
        'last_lead_prompt_at',
        'metadata',
    ];

    protected $casts = [
        'started_at' => 'datetime',
        'last_activity_at' => 'datetime',
        'last_lead_prompt_at' => 'datetime',
        'is_returning' => 'boolean',
        'metadata' => 'array',
    ];

    public function messages(): HasMany
    {
        return $this->hasMany(ChatMessage::class)->orderBy('id');
    }

    public function formSubmissions(): HasMany
    {
        return $this->hasMany(LeadFormSubmission::class)->latest();
    }
}
