#!/usr/bin/env bash
# Pre-push acceptance gate for cop-com-cms.
# Runs without a database or web server. Exit status 0 = pass.

set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PASS=0
FAIL=0

ok()   { echo "[ok] $*";   ((PASS++)) || true; }
fail() { echo "[FAIL] $*"; ((FAIL++)) || true; }

echo "=== cop-com-cms pre-push acceptance ==="
echo

# ── 1. PHP version ───────────────────────────────────────────────────────────
PHP_VER=$(php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;')
PHP_MAJOR=$(php -r 'echo PHP_MAJOR_VERSION;')
PHP_MINOR=$(php -r 'echo PHP_MINOR_VERSION;')
if [[ "$PHP_MAJOR" -gt 8 ]] || [[ "$PHP_MAJOR" -eq 8 && "$PHP_MINOR" -ge 2 ]]; then
    ok "PHP version ${PHP_VER} >= 8.2"
else
    fail "PHP version ${PHP_VER} < 8.2 (required: 8.2+)"
fi

# ── 2. PHP syntax lint ───────────────────────────────────────────────────────
LINT_ERRORS=0
while IFS= read -r -d '' file; do
    if ! php -l "$file" > /dev/null 2>&1; then
        fail "Syntax error: $file"
        ((LINT_ERRORS++)) || true
    fi
done < <(find "$ROOT/src" "$ROOT/public" "$ROOT/scripts" -name '*.php' -print0)

if [[ "$LINT_ERRORS" -eq 0 ]]; then
    ok "PHP syntax lint passed"
fi

# ── 3. Required files present ────────────────────────────────────────────────
REQUIRED_FILES=(
    "src/bootstrap.php"
    "src/config.php"
    "src/env.php"
    "src/routes.php"
    "database/schema.sql"
    ".env.example"
    ".htaccess"
    "public/index.php"
    "public/install.php"
    "public/install/index.php"
    "public/.htaccess"
)

for rel in "${REQUIRED_FILES[@]}"; do
    if [[ -f "$ROOT/$rel" ]]; then
        ok "Exists: $rel"
    else
        fail "Missing: $rel"
    fi
done

# ── 4. .env.example keys ─────────────────────────────────────────────────────
REQUIRED_KEYS=(
    DB_HOST DB_PORT DB_DATABASE DB_USERNAME DB_PASSWORD
    APP_URL APP_NAME APP_ENV APP_DEBUG APP_INSTALLED
    ADMIN_PATH
)

for key in "${REQUIRED_KEYS[@]}"; do
    if grep -q "^${key}=" "$ROOT/.env.example"; then
        ok ".env.example has $key"
    else
        fail ".env.example missing $key"
    fi
done

# ── 5. Shared-hosting fallback hardening ─────────────────────────────────────
if grep -q '^Options -Indexes$' "$ROOT/.htaccess"; then
    ok "Root .htaccess disables autoindex"
else
    fail "Root .htaccess missing 'Options -Indexes'"
fi

if grep -q '^DirectoryIndex index.php$' "$ROOT/.htaccess"; then
    ok "Root .htaccess declares index.php as directory index"
else
    fail "Root .htaccess missing 'DirectoryIndex index.php'"
fi

# ── 6. Admin-layout regression sweep (no DB required) ────────────────────────
echo
echo "--- Admin layout regression sweep ---"
if php "$ROOT/scripts/admin-layout-regression-sweep.php"; then
    ok "Admin layout regression sweep passed"
else
    fail "Admin layout regression sweep failed"
fi

# ── Summary ───────────────────────────────────────────────────────────────────
echo
echo "=== Results: ${PASS} passed, ${FAIL} failed ==="

if [[ "$FAIL" -gt 0 ]]; then
    exit 1
fi
