<?php
declare(strict_types=1);

namespace PpPhp\Core\Assets;

final class AssetManifest
{
    public function __construct(
        private readonly string $manifestId,
        private readonly array $packages = [],
        private readonly array $assets = [],
        private readonly array $metadata = []
    ) {}

    public static function create(string $manifestId, array $packages, array $assets, array $metadata = []): self
    {
        return new self($manifestId, $packages, $assets, $metadata);
    }

    public function toArray(): array
    {
        return [
            'manifestId' => $this->manifestId,
            'packages' => $this->packages,
            'assets' => $this->assets,
            'metadata' => $this->metadata,
            'summary' => [
                'packages' => count($this->packages),
                'assets' => count($this->assets),
                'css' => count(array_filter($this->assets, static fn (array $asset): bool => ($asset['type'] ?? '') === 'css')),
                'js' => count(array_filter($this->assets, static fn (array $asset): bool => ($asset['type'] ?? '') === 'js')),
                'fonts' => count(array_filter($this->assets, static fn (array $asset): bool => ($asset['type'] ?? '') === 'font')),
                'images' => count(array_filter($this->assets, static fn (array $asset): bool => in_array(($asset['type'] ?? ''), ['image', 'svg'], true))),
            ],
        ];
    }
}
