<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * One on-demand probe request. See migration docblock for the security
 * model. This is the queue row — the agent picks it up via long-poll
 * and posts the result back.
 */
class AgentProbe extends Model
{
    public const STATUS_PENDING  = 'pending';
    public const STATUS_RUNNING  = 'running';
    public const STATUS_SUCCESS  = 'success';
    public const STATUS_FAILED   = 'failed';
    public const STATUS_EXPIRED  = 'expired';

    protected $fillable = [
        'server_id', 'ticket_id', 'requested_by_user_id',
        'probe_type', 'params', 'status',
        'result_text', 'result_summary', 'exit_code', 'duration_ms',
        'error_message',
        'queued_at', 'dispatched_at', 'completed_at', 'expires_at',
        'audit_command',
    ];

    protected $casts = [
        'params'         => 'array',
        'queued_at'      => 'datetime',
        'dispatched_at'  => 'datetime',
        'completed_at'   => 'datetime',
        'expires_at'     => 'datetime',
        'exit_code'      => 'integer',
        'duration_ms'    => 'integer',
    ];

    public function server(): BelongsTo  { return $this->belongsTo(Server::class); }
    public function ticket(): BelongsTo  { return $this->belongsTo(Ticket::class); }

    public function isTerminal(): bool
    {
        return in_array($this->status, [
            self::STATUS_SUCCESS, self::STATUS_FAILED, self::STATUS_EXPIRED,
        ], true);
    }
}
