<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Build;

use PpPhp\Core\Designer\Build\Builders\ApplicationBuilder;
use PpPhp\Core\Designer\Build\Builders\AssetBuilder;
use PpPhp\Core\Designer\Build\Builders\ManifestBuilder;
use PpPhp\Core\Designer\Build\Builders\RouteBuilder;
use PpPhp\Core\Designer\Build\Contracts\BuildManifest;
use PpPhp\Core\Designer\Generation\Contracts\GeneratedApplication;

final class DesignerBuildPipeline
{
    public function build(GeneratedApplication $application): BuildManifest
    {
        $app = $application->toArray();
        $artifacts = [];

        $artifacts = array_merge($artifacts, (new ApplicationBuilder())->build($application));
        $artifacts[] = (new RouteBuilder())->build((array) ($app['routes'] ?? []));
        $artifacts = array_merge($artifacts, (new AssetBuilder())->build((array) ($app['assets'] ?? [])));

        return (new ManifestBuilder())->build(
            $application->applicationId(),
            $artifacts,
            (array) ($app['routes'] ?? []),
            (array) ($app['assets'] ?? []),
            ['source' => 'designer-build-pipeline', 'builtAt' => date('c')]
        );
    }
}
