<?php

declare(strict_types=1);

namespace App\Models;

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

/**
 * 宿主官方系统更新检查快照（单例表 host_system_update_checks）。
 *
 * NOTE: Support Center Phase 1 不提供 manifest API；后续步骤从已下载 ZIP 内 manifest.json 解析。
 * 本表不存储 download token、host_token、host_key 等密钥。
 *
 * @property string|null $product_code
 * @property string|null $current_version
 * @property int|null $current_update_version
 * @property string|null $latest_version
 * @property int|null $latest_update_version
 * @property string|null $release_channel
 * @property bool $update_available
 * @property string|null $package_id
 * @property string|null $package_type
 * @property string|null $package_version
 * @property int|null $package_size
 * @property string|null $checksum
 * @property string|null $checksum_algorithm
 * @property string|null $signature
 * @property array<string, mixed>|null $release_notes
 * @property array<string, mixed>|null $requirements
 * @property string|null $min_upgradable_version
 * @property bool|null $entitled
 * @property string|null $entitlement_message
 * @property string|null $last_check_status
 * @property string|null $last_check_message
 * @property string|null $last_check_error_code
 * @property string|null $last_request_id
 * @property Carbon|null $last_checked_at
 * @property array<string, mixed>|null $raw_snapshot
 */
class HostSystemUpdateCheck extends Model
{
    public const SINGLETON_KEY = 'default';

    protected $table = 'host_system_update_checks';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'singleton_key',
        'product_code',
        'current_version',
        'current_update_version',
        'latest_version',
        'latest_update_version',
        'release_channel',
        'update_available',
        'package_id',
        'package_type',
        'package_version',
        'package_size',
        'checksum',
        'checksum_algorithm',
        'signature',
        'release_notes',
        'requirements',
        'min_upgradable_version',
        'entitled',
        'entitlement_message',
        'last_check_status',
        'last_check_message',
        'last_check_error_code',
        'last_request_id',
        'last_checked_at',
        'raw_snapshot',
    ];

    protected function casts(): array
    {
        return [
            'current_update_version' => 'integer',
            'latest_update_version' => 'integer',
            'update_available' => 'boolean',
            'package_size' => 'integer',
            'release_notes' => 'array',
            'requirements' => 'array',
            'entitled' => 'boolean',
            'last_checked_at' => 'datetime',
            'raw_snapshot' => 'array',
        ];
    }

    public static function singleton(): self
    {
        /** @var self $record */
        $record = self::query()->firstOrCreate(
            ['singleton_key' => self::SINGLETON_KEY],
            [
                'product_code' => 'dberp',
                'update_available' => false,
            ],
        );

        return $record;
    }
}
