<?php

declare(strict_types=1);

namespace PpPhp\Core\Manifest;

final class ManifestReader
{
    public function read(string $manifestPath): Manifest
    {
        if (!is_file($manifestPath)) {
            throw new \RuntimeException('Manifest file not found: ' . $manifestPath);
        }

        $raw = (string) file_get_contents($manifestPath);
        $data = json_decode($raw, true);

        if (!is_array($data)) {
            throw new \RuntimeException('Invalid JSON manifest: ' . $manifestPath);
        }

        return new Manifest($data, $manifestPath);
    }

    public function tryRead(string $manifestPath): ?Manifest
    {
        try {
            return $this->read($manifestPath);
        } catch (\Throwable) {
            return null;
        }
    }
}
