#!/usr/bin/env bash
# Captures the runtime state that masks "APP_DEBUG=true didn't render Whoops":
# OPcache, config-cache, deployed git commit, .env vs runtime config, PHP limits.
# stdout is captured by run-all.sh — print plainly.

set -u

cd "$(dirname "$0")/../.." || exit 1

echo "--- Git ---"
git log --oneline -5 2>&1 | head -5
echo ""
echo "Working tree clean: $(git diff --quiet 2>/dev/null && echo yes || echo no)"
echo ""

echo "--- PHP ---"
php -v 2>&1 | head -3
echo ""
echo "memory_limit:        $(php -r 'echo ini_get("memory_limit");')"
echo "max_execution_time:  $(php -r 'echo ini_get("max_execution_time");')"
echo "display_errors:      $(php -r 'echo ini_get("display_errors");')"
echo "log_errors:          $(php -r 'echo ini_get("log_errors");')"
echo "error_log:           $(php -r 'echo ini_get("error_log") ?: "(SAPI default — Apache error log)";')"
echo ""

echo "--- OPcache ---"
php -r '
$op = function_exists("opcache_get_status") ? @opcache_get_status(false) : null;
if (!$op) {
    echo "opcache: disabled or not loaded\n";
} else {
    echo "opcache: enabled\n";
    echo "  cached_scripts:   " . ($op["opcache_statistics"]["num_cached_scripts"] ?? "n/a") . "\n";
    echo "  hits:             " . ($op["opcache_statistics"]["hits"] ?? "n/a") . "\n";
    echo "  misses:           " . ($op["opcache_statistics"]["misses"] ?? "n/a") . "\n";
    echo "  validate_ts:      " . ini_get("opcache.validate_timestamps") . "\n";
    echo "  revalidate_freq:  " . ini_get("opcache.revalidate_freq") . " (sec)\n";
}
echo "\nNOTE: With Apache + mod_php, OPcache lives PER-PROCESS in each Apache child.\n";
echo "CLI runs (artisan / this script) have their own OPcache context.\n";
echo "If you want to clear web-server OPcache, restart Apache: systemctl restart apache2 (or httpd)\n";
'
echo ""

echo "--- Laravel config-cache state ---"
ls -la bootstrap/cache/*.php 2>/dev/null || echo "(no cached files)"
echo ""

if [[ -f bootstrap/cache/config.php ]]; then
    echo "*** WARNING: bootstrap/cache/config.php EXISTS ***"
    echo "*** This will mask any .env changes (including APP_DEBUG=true) ***"
    echo "*** Web requests use the cached snapshot until 'php artisan config:clear' is run ***"
    echo ""
    echo "Cached APP_DEBUG value:"
    php -r '
    $cfg = require "bootstrap/cache/config.php";
    echo "  app.debug = " . var_export($cfg["app"]["debug"] ?? "MISSING", true) . "\n";
    echo "  app.env   = " . var_export($cfg["app"]["env"] ?? "MISSING", true) . "\n";
    '
fi
echo ""

echo "--- .env ---"
if [[ -f .env ]]; then
    echo ".env exists. Relevant lines:"
    grep -E '^(APP_DEBUG|APP_ENV|APP_URL|LOG_CHANNEL|LOG_LEVEL|SESSION_DRIVER|CACHE_DRIVER|DB_CONNECTION)' .env 2>/dev/null \
        | sed 's/=.*KEY.*/=***REDACTED***/'
else
    echo ".env NOT FOUND (this would itself cause a 500 — check deploy)"
fi
echo ""

echo "--- Runtime config (via artisan) ---"
echo "(this is what the LIVE webserver sees — including any cache override)"
php artisan tinker --execute="
echo 'app.debug=' . var_export(config('app.debug'), true) . PHP_EOL;
echo 'app.env=' . config('app.env') . PHP_EOL;
echo 'app.url=' . config('app.url') . PHP_EOL;
echo 'session.driver=' . config('session.driver') . PHP_EOL;
echo 'cache.default=' . config('cache.default') . PHP_EOL;
echo 'log.default=' . config('logging.default') . PHP_EOL;
" 2>&1 | tail -10
echo ""

echo "--- Deployed code matches expected fix? ---"
if grep -q 'try {' config/scribe.php 2>/dev/null && grep -q 'degraded mode' config/scribe.php 2>/dev/null; then
    echo "config/scribe.php: HAS 3edbd041 fix (try/catch + degraded-mode log)"
else
    echo "config/scribe.php: MISSING 3edbd041 fix — deploy hasn't picked up the latest commit"
fi
echo ""

echo "--- Storage permissions ---"
ls -la storage/logs/laravel.log 2>/dev/null || echo "laravel.log not present yet"
ls -ld storage/framework/views/ storage/framework/sessions/ storage/framework/cache/ 2>/dev/null
echo ""
