<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Build\Builders;

use PpPhp\Core\Designer\Build\Contracts\BuildArtifact;

final class AssetBuilder
{
    /** @return array<int, BuildArtifact> */
    public function build(array $assets): array
    {
        $artifacts = [];
        $manifestAssets = (array) ($assets['manifest']['assets'] ?? $assets['assets'] ?? []);

        $artifacts[] = BuildArtifact::create(
            'assets_manifest',
            'assets',
            'assets/manifest.json',
            json_encode($assets, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
            ['assets' => count($manifestAssets)]
        );

        $artifacts[] = BuildArtifact::create(
            'app_css',
            'asset',
            'assets/css/app.css',
            "/* Generated PPphp application CSS */\nbody{font-family:var(--pp-font,Inter,system-ui,sans-serif);margin:0;background:var(--pp-bg,#f8fafc);color:var(--pp-text,#0f172a);}\n",
            ['assetType' => 'css']
        );

        $artifacts[] = BuildArtifact::create(
            'app_js',
            'asset',
            'assets/js/app.js',
            "/* Generated PPphp application JS */\nwindow.PpGeneratedApp={boot:function(){return true;}};\n",
            ['assetType' => 'js']
        );

        return $artifacts;
    }
}
