<?php

declare(strict_types=1);

namespace SeoScope\Updates;

/**
 * Optional latest-version metadata from the public releases.json endpoint.
 */
final class ReleaseInfo
{
    public function __construct(
        public string $version,
        public ?string $releasedAt,
        public ?string $changelogUrl
    ) {
    }

    /**
     * @param array<string,mixed> $j
     */
    public static function fromJsonArray(array $j): ?self
    {
        $v = $j['version'] ?? null;
        if (!is_string($v) || trim($v) === '') {
            return null;
        }
        $v = trim($v);
        $at = $j['released_at'] ?? null;
        $url = $j['changelog_url'] ?? null;
        return new self(
            $v,
            is_string($at) && $at !== '' ? $at : null,
            is_string($url) && $url !== '' ? $url : null
        );
    }
}
