<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class BotRuntimeState extends Model
{
    protected $table = 'bot_runtime_state';

    protected $fillable = ['worker', 'status', 'last_heartbeat_at', 'meta'];

    protected function casts(): array
    {
        return [
            'meta' => 'array',
            'last_heartbeat_at' => 'datetime',
        ];
    }

    public static function heartbeat(string $worker, string $status = 'running', array $meta = []): void
    {
        static::updateOrCreate(
            ['worker' => $worker],
            ['status' => $status, 'last_heartbeat_at' => now(), 'meta' => $meta]
        );
    }
}
