<?php

declare(strict_types=1);

namespace PpPhp\Core\Diagnostics\Metadata;

final class MetadataFactory
{
    public static function fromArray(array $data, string $path = ''): MetadataInterface
    {
        $type = trim((string) ($data['type'] ?? 'manifest'));
        $id = trim((string) ($data['id'] ?? basename($path) ?: 'unknown'));
        $label = trim((string) ($data['label'] ?? $data['name'] ?? $id));
        $version = trim((string) ($data['version'] ?? '1.0.0'));
        $dependencies = is_array($data['dependencies'] ?? null) ? $data['dependencies'] : [];
        $capabilities = is_array($data['capabilities'] ?? null) ? $data['capabilities'] : [];
        $assets = is_array($data['assets'] ?? null) ? $data['assets'] : [];

        return match ($type) {
            'package', 'library', 'ui', 'ui-foundation', 'platform-bundle' => new PackageMetadata($id, $type, $label, $version, $path, $dependencies, $capabilities, $assets, $data),
            'module' => new ModuleMetadata($id, $type, $label, $version, $path, $dependencies, $capabilities, $assets, $data),
            'plugin' => new PluginMetadata($id, $type, $label, $version, $path, $dependencies, $capabilities, $assets, $data),
            'theme' => new ThemeMetadata($id, $type, $label, $version, $path, $dependencies, $capabilities, $assets, $data),
            'asset' => new AssetMetadata($id, $type, $label, $version, $path, $dependencies, $capabilities, $assets, $data),
            'component' => new ComponentMetadata($id, $type, $label, $version, $path, $dependencies, $capabilities, $assets, $data),
            'designer' => new DesignerMetadata($id, $type, $label, $version, $path, $dependencies, $capabilities, $assets, $data),
            'documentation' => new DocumentationMetadata($id, $type, $label, $version, $path, $dependencies, $capabilities, $assets, $data),
            default => new ManifestMetadata($id, $type, $label, $version, $path, $dependencies, $capabilities, $assets, $data),
        };
    }
}
