<?php
/* ============================================================================
 *  app_version.php  —  returns a single token that changes whenever ANY .php
 *  file in the project is updated. Used by the auto-update watcher so open
 *  pages reload themselves after you deploy a change.
 * ==========================================================================*/

if (!function_exists('app_version')) {
    function app_version(): string {
        $root = dirname(__DIR__); // project root (this file lives in config/)

        /* light cache so we don't rescan on every poll (10s) */
        $cache = sys_get_temp_dir() . '/seven_app_ver.txt';
        if (is_file($cache) && (time() - filemtime($cache) < 10)) {
            $c = trim((string) @file_get_contents($cache));
            if ($c !== '') return $c;
        }

        $latest = 0;
        try {
            $it = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS)
            );
            foreach ($it as $f) {
                if ($f->isFile() && strtolower($f->getExtension()) === 'php') {
                    $m = $f->getMTime();
                    if ($m > $latest) $latest = $m;
                }
            }
        } catch (Throwable $e) {
            $latest = (int) (@filemtime($root . '/auth/login.php') ?: 0);
        }

        @file_put_contents($cache, (string) $latest);
        return (string) $latest;
    }
}