<?php

declare(strict_types=1);

namespace App\Models;

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

/**
 * 宿主官方系统更新执行任务（表 host_system_update_executions）。
 *
 * @property string $execution_no
 * @property string|null $package_id
 * @property int|null $host_system_update_package_id
 * @property string|null $product_code
 * @property string|null $from_version
 * @property int|null $from_update_version
 * @property string|null $to_version
 * @property int|null $to_update_version
 * @property string|null $release_channel
 * @property string|null $package_type
 * @property string|null $status
 * @property string|null $current_step
 * @property int $progress_percent
 * @property Carbon|null $started_at
 * @property Carbon|null $finished_at
 * @property Carbon|null $failed_at
 * @property Carbon|null $cancelled_at
 * @property string|null $rollback_status
 * @property Carbon|null $rollback_started_at
 * @property Carbon|null $rollback_finished_at
 * @property string|null $error_code
 * @property string|null $error_message
 * @property int|null $operator_id
 * @property string|null $operator_name
 * @property Carbon|null $locked_at
 * @property string|null $lock_owner
 * @property array<string, mixed>|null $raw_manifest
 */
class HostSystemUpdateExecution extends Model
{
    protected $table = 'host_system_update_executions';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'execution_no',
        'package_id',
        'host_system_update_package_id',
        'product_code',
        'from_version',
        'from_update_version',
        'to_version',
        'to_update_version',
        'release_channel',
        'package_type',
        'status',
        'current_step',
        'progress_percent',
        'started_at',
        'finished_at',
        'failed_at',
        'cancelled_at',
        'rollback_status',
        'rollback_started_at',
        'rollback_finished_at',
        'error_code',
        'error_message',
        'operator_id',
        'operator_name',
        'locked_at',
        'lock_owner',
        'raw_manifest',
    ];

    protected function casts(): array
    {
        return [
            'from_update_version' => 'integer',
            'to_update_version' => 'integer',
            'progress_percent' => 'integer',
            'started_at' => 'datetime',
            'finished_at' => 'datetime',
            'failed_at' => 'datetime',
            'cancelled_at' => 'datetime',
            'rollback_started_at' => 'datetime',
            'rollback_finished_at' => 'datetime',
            'locked_at' => 'datetime',
            'raw_manifest' => 'array',
            'operator_id' => 'integer',
        ];
    }

    public function package(): BelongsTo
    {
        return $this->belongsTo(HostSystemUpdatePackage::class, 'host_system_update_package_id');
    }

    public function logs(): HasMany
    {
        return $this->hasMany(HostSystemUpdateExecutionLog::class, 'execution_id');
    }

    public function backups(): HasMany
    {
        return $this->hasMany(HostSystemUpdateBackup::class, 'execution_id');
    }
}
