<?php
namespace CAE\Lib;

class View
{
    public static function render(string $view, array $data = []): void
    {
        extract($data, EXTR_SKIP);
        $file = VIEWS_PATH . '/' . ltrim($view, '/') . '.php';
        if (!file_exists($file)) {
            throw new \RuntimeException("View not found: {$view}");
        }
        require $file;
    }

    public static function partial(string $template, array $data = []): void
    {
        self::render('layout/' . $template, $data);
    }

    public static function flash(string $type, string $message): void
    {
        $_SESSION['flash'] = compact('type', 'message');
    }

    public static function getFlash(): ?array
    {
        if (!empty($_SESSION['flash'])) {
            $flash = $_SESSION['flash'];
            unset($_SESSION['flash']);
            return $flash;
        }
        return null;
    }

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