<?php

declare(strict_types=1);

namespace App\Core;

use RuntimeException;

final class View
{
    public static function render(string $view, array $data = [], string $layout = 'layouts/app'): string
    {
        $viewPath = BASE_PATH . '/app/Views/' . $view . '.php';
        $layoutPath = BASE_PATH . '/app/Views/' . $layout . '.php';

        if (!is_file($viewPath)) {
            throw new RuntimeException("View not found: {$view}");
        }

        if (!is_file($layoutPath)) {
            throw new RuntimeException("Layout not found: {$layout}");
        }

        extract($data, EXTR_SKIP);

        ob_start();
        require $viewPath;
        $content = ob_get_clean();

        ob_start();
        require $layoutPath;

        return (string) ob_get_clean();
    }
}
