<?php

declare(strict_types=1);

namespace App\Framework\Process\ValueObjects\Npm;

/**
 * NPM Package Information.
 */
final readonly class NpmPackage
{
    public function __construct(
        public string $name,
        public string $currentVersion,
        public ?string $wantedVersion = null,
        public ?string $latestVersion = null,
        public ?string $type = null, // 'dependencies' or 'devDependencies'
        public bool $isOutdated = false
    ) {
    }

    /**
     * Erstellt Package aus NPM-Output.
     */
    public static function fromNpmOutput(
        string $name,
        string $currentVersion,
        ?string $wantedVersion = null,
        ?string $latestVersion = null,
        ?string $type = null
    ): self {
        return new self(
            name: $name,
            currentVersion: $currentVersion,
            wantedVersion: $wantedVersion,
            latestVersion: $latestVersion,
            type: $type,
            isOutdated: $latestVersion !== null && $latestVersion !== $currentVersion
        );
    }

    /**
     * Konvertiert zu Array.
     *
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'name' => $this->name,
            'current_version' => $this->currentVersion,
            'wanted_version' => $this->wantedVersion,
            'latest_version' => $this->latestVersion,
            'type' => $this->type,
            'is_outdated' => $this->isOutdated,
        ];
    }
}
