<?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\HasMany;
use Illuminate\Support\Carbon;

/**
 * 宿主官方系统更新包元数据（表 host_system_update_packages）。
 *
 * NOTE: 不持久化 download_token、host_token、host_key；manifest.json 在后续步骤从 ZIP 内解析。
 *
 * @property string|null $package_id
 * @property string|null $package_no
 * @property string|null $product_code
 * @property string|null $version
 * @property string|null $release_channel
 * @property string|null $package_type
 * @property string|null $package_name
 * @property int|null $package_size
 * @property string|null $checksum
 * @property string|null $checksum_algorithm
 * @property string|null $signature
 * @property string|null $download_status
 * @property string|null $download_message
 * @property string|null $download_error_code
 * @property string|null $download_request_id
 * @property Carbon|null $token_expires_at
 * @property string|null $local_path
 * @property string|null $local_filename
 * @property int|null $local_size
 * @property bool $verified
 * @property Carbon|null $verified_at
 * @property Carbon|null $downloaded_at
 * @property array<string, mixed>|null $raw_payload
 * @property array<string, mixed>|null $manifest
 * @property string|null $manifest_status
 * @property string|null $manifest_message
 * @property string|null $manifest_error_code
 * @property Carbon|null $manifest_parsed_at
 */
class HostSystemUpdatePackage extends Model
{
    public const STATUS_TOKEN_ISSUED = 'token_issued';

    public const STATUS_DOWNLOADING = 'downloading';

    public const STATUS_DOWNLOADED = 'downloaded';

    public const STATUS_VERIFIED = 'verified';

    public const STATUS_VERIFICATION_FAILED = 'verification_failed';

    public const STATUS_FAILED = 'failed';

    public const MANIFEST_STATUS_VALID = 'valid';

    public const MANIFEST_STATUS_INVALID = 'invalid';

    public const MANIFEST_STATUS_UNSUPPORTED = 'unsupported';

    public const MANIFEST_STATUS_PARSE_FAILED = 'parse_failed';

    protected $table = 'host_system_update_packages';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'package_id',
        'package_no',
        'product_code',
        'version',
        'release_channel',
        'package_type',
        'package_name',
        'package_size',
        'checksum',
        'checksum_algorithm',
        'signature',
        'download_status',
        'download_message',
        'download_error_code',
        'download_request_id',
        'token_expires_at',
        'local_path',
        'local_filename',
        'local_size',
        'verified',
        'verified_at',
        'downloaded_at',
        'raw_payload',
        'manifest',
        'manifest_status',
        'manifest_message',
        'manifest_error_code',
        'manifest_parsed_at',
    ];

    protected function casts(): array
    {
        return [
            'package_size' => 'integer',
            'local_size' => 'integer',
            'verified' => 'boolean',
            'token_expires_at' => 'datetime',
            'verified_at' => 'datetime',
            'downloaded_at' => 'datetime',
            'raw_payload' => 'array',
            'manifest' => 'array',
            'manifest_parsed_at' => 'datetime',
        ];
    }

    public function executions(): HasMany
    {
        return $this->hasMany(HostSystemUpdateExecution::class, 'host_system_update_package_id');
    }
}
