<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

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');
    }
}
