<?php

namespace App\Core;

class View
{
    private static string $basePath = '';

    public static function setBasePath(string $path): void
    {
        self::$basePath = rtrim($path, '/');
    }

    public static function render(string $template, array $data = []): void
    {
        extract($data);

        $flashMessages = $_SESSION['flash'] ?? [];
        unset($_SESSION['flash']);

        $contentFile = self::$basePath . "/templates/{$template}.php";

        if (!file_exists($contentFile)) {
            throw new \RuntimeException("Template not found: {$template}");
        }

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

        require self::$basePath . '/templates/layout/main.php';
    }

    public static function renderPartial(string $template, array $data = []): void
    {
        extract($data);
        require self::$basePath . "/templates/{$template}.php";
    }

    public static function escape(mixed $value): string
    {
        return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
    }
}
