<?php
declare(strict_types=1);

/** Emits GA4 config + shared analytics.js for PHP views. */
final class AnalyticsHead {
    public static function render(string $platform = 'crashes'): void {
        $enabled = (bool) cfg('analytics.enabled', false);
        $measurementId = trim((string) cfg('analytics.measurement_id', ''));
        if (!$enabled || $measurementId === '') {
            return;
        }
        $basePath = Auth::basePath();
        $config = [
            'measurementId' => $measurementId,
            'platform' => $platform,
            'basePath' => $basePath,
            'debug' => (bool) cfg('analytics.debug', false),
        ];
        $json = json_encode($config, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP);
        if ($json === false) {
            return;
        }
        echo '<script>window.ANDROIDCAST_ANALYTICS=', $json, ';</script>', "\n";
        echo '<script src="', h($basePath), '/assets/js/analytics.js" defer></script>', "\n";
    }
}
