#!/usr/bin/env bash
set -u

APP_DIR="${APP_DIR:-/workspace/digest}"
DOMAIN_URL="${DOMAIN_URL:-https://www.tonki.dev/digest/digest-archive/2026-06-11/}"

section() {
  printf '\n== %s ==\n' "$1"
}

run() {
  printf '+ %s\n' "$*"
  "$@"
}

section "Versions"
run php -v
run nginx -v
run node -v
run npm -v
run jq --version
run curl --version
run git --version
run claude --version
run nordvpn --version

section "User and time"
run id agent
run groups agent
run timedatectl

section "Services"
run systemctl status nginx --no-pager
run systemctl status php8.3-fpm --no-pager
run systemctl status nordvpnd --no-pager
run systemctl status digest-orchestrator.timer --no-pager
run systemctl status digest-vpn-health.timer --no-pager
run systemctl list-timers --all

section "Nginx"
run sudo nginx -t
run curl -I "$DOMAIN_URL"

section "Claude MCP"
run claude mcp list

section "VPN"
run sg nordvpn -c "nordvpn status"
run sg nordvpn -c "nordvpn settings"
run "${APP_DIR}/vpn-health.sh" check

section "Permissions"
run stat -c '%A %U:%G %n' /workspace "${APP_DIR}" "${APP_DIR}/SUGGESTIONS-FROM-USER.MD"
if sudo -u www-data test -w "${APP_DIR}/SUGGESTIONS-FROM-USER.MD"; then
  echo "www-data can write suggestions"
else
  echo "www-data cannot write suggestions"
fi

section "Product Hunt"
bash -lc "cd '${APP_DIR}' && ./DIGESTS/0-PRODUCTHUNT/fetch-producthunt.sh \"\$(date +%Y-%m-%d)\" | head -40"

section "Link validation sample"
bash -lc "cd '${APP_DIR}' && ./validate-links.sh digest-archive/2026-06-11/index.php"

