<?php
declare(strict_types=1);

namespace App\Core;

final class View
{
    public static function render(string $template, array $data = [], ?string $layout = 'layouts/app'): void
    {
        $file = VIEWS_PATH . '/' . $template . '.php';
        if (!is_file($file)) {
            throw new \RuntimeException('View not found: ' . $template);
        }
        $viewVars = $data;
        extract($viewVars, EXTR_OVERWRITE);
        ob_start();
        include $file;
        $content = ob_get_clean();
        if ($layout) {
            $layoutFile = VIEWS_PATH . '/' . $layout . '.php';
            include $layoutFile;
            return;
        }
        echo $content;
    }

    public static function partial(string $name, array $data = []): void
    {
        $file = VIEWS_PATH . '/partials/' . $name . '.php';
        if (!is_file($file)) {
            return;
        }
        extract($data, EXTR_OVERWRITE);
        include $file;
    }
}
