<?php

declare(strict_types=1);

namespace App\Models;

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

/**
 * 宿主官方系统更新执行步骤日志（表 host_system_update_execution_logs）。
 *
 * @property int $execution_id
 * @property string|null $step
 * @property string|null $status
 * @property string|null $message
 * @property array<string, mixed>|null $context
 * @property Carbon|null $started_at
 * @property Carbon|null $finished_at
 * @property int|null $duration_ms
 * @property string|null $error_code
 * @property string|null $error_message
 */
class HostSystemUpdateExecutionLog extends Model
{
    public const STATUS_STARTED = 'started';

    public const STATUS_COMPLETED = 'completed';

    public const STATUS_FAILED = 'failed';

    public const STATUS_SKIPPED = 'skipped';

    public const STATUS_TRANSITION = 'transition';

    protected $table = 'host_system_update_execution_logs';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'execution_id',
        'step',
        'status',
        'message',
        'context',
        'started_at',
        'finished_at',
        'duration_ms',
        'error_code',
        'error_message',
    ];

    protected function casts(): array
    {
        return [
            'execution_id' => 'integer',
            'context' => 'array',
            'started_at' => 'datetime',
            'finished_at' => 'datetime',
            'duration_ms' => 'integer',
        ];
    }

    public function execution(): BelongsTo
    {
        return $this->belongsTo(HostSystemUpdateExecution::class, 'execution_id');
    }
}
