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

/**
 * 官方插件安装包元数据（不持久化 download_token）。
 *
 * @property string $plugin_code
 * @property string|null $version
 * @property string|null $package_id
 * @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 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
 */
class HostPluginPackage extends Model
{
    public const STATUS_PENDING = 'pending';

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

    protected $table = 'host_plugin_packages';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'plugin_code',
        'version',
        'package_id',
        'package_name',
        'package_size',
        'checksum',
        'checksum_algorithm',
        'signature',
        'download_status',
        'download_message',
        'download_error_code',
        'download_request_id',
        'local_path',
        'local_filename',
        'local_size',
        'verified',
        'verified_at',
        'downloaded_at',
        'raw_payload',
    ];

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