#!/bin/bash
# Pre-deploy checklist — run before every deployment
set -e

echo "╔══════════════════════════════════════════╗"
echo "║   El Captain — Pre-Deploy Checklist      ║"
echo "╚══════════════════════════════════════════╝"
echo ""

PASS=0
FAIL=0
WARN=0

check_pass() { echo "  ✓ $1"; PASS=$((PASS+1)); }
check_fail() { echo "  ✗ $1"; FAIL=$((FAIL+1)); }
check_warn() { echo "  ⚠ $1"; WARN=$((WARN+1)); }

# 1. Syntax check all PHP files
echo "▶ PHP Syntax Check"
SYNTAX_ERRORS=$(find app database routes -name "*.php" -exec php -l {} \; 2>&1 | grep -c "Parse error" || true)
if [ "$SYNTAX_ERRORS" -eq 0 ]; then
    check_pass "All PHP files pass syntax check"
else
    check_fail "$SYNTAX_ERRORS files have syntax errors"
fi

# 2. FK verification
echo ""
echo "▶ Foreign Key Order Verification"
if php scripts/verify-fk-order.php > /dev/null 2>&1; then
    check_pass "All FK references point to existing tables"
else
    check_fail "FK order issues detected — run: php scripts/verify-fk-order.php"
fi

# 3. .env.example completeness
echo ""
echo "▶ Environment Configuration"
if [ -f .env.example ]; then
    check_pass ".env.example exists"
else
    check_fail ".env.example missing"
fi

if grep -q "APP_KEY=" .env.example; then
    check_pass "APP_KEY placeholder present"
else
    check_fail "APP_KEY missing from .env.example"
fi

if grep -q "DB_PASSWORD=" .env.example; then
    check_pass "DB_PASSWORD placeholder present"
else
    check_fail "DB credentials missing from .env.example"
fi

# 4. Docker build readiness
echo ""
echo "▶ Docker Configuration"
if [ -f Dockerfile ]; then
    check_pass "Dockerfile exists"
else
    check_fail "Dockerfile missing"
fi

if [ -f captain-definition ]; then
    check_pass "captain-definition exists"
else
    check_fail "captain-definition missing"
fi

if [ -f docker/entrypoint.sh ]; then
    check_pass "docker/entrypoint.sh exists"
else
    check_fail "docker/entrypoint.sh missing"
fi

if [ -f docker/supervisor/supervisord.conf ]; then
    check_pass "Supervisor config exists"
else
    check_fail "Supervisor config missing"
fi

# 5. Required directories
echo ""
echo "▶ Directory Structure"
for dir in "app/Domain" "database/migrations" "database/seeders" "resources/views"; do
    if [ -d "$dir" ]; then
        check_pass "$dir exists"
    else
        check_fail "$dir missing"
    fi
done

# 6. No debug/dev artifacts
echo ""
echo "▶ Production Safety"
if grep -rq "dd(" app/ --include="*.php" 2>/dev/null; then
    check_warn "dd() calls found in app/ — remove before deploy"
else
    check_pass "No dd() calls in app/"
fi

if grep -rq "dump(" app/ --include="*.php" 2>/dev/null; then
    check_warn "dump() calls found in app/"
else
    check_pass "No dump() calls in app/"
fi

if grep -rq "Ray::" app/ --include="*.php" 2>/dev/null; then
    check_warn "Ray:: debug calls found in app/"
else
    check_pass "No Ray debug calls"
fi

# 7. Event → Listener completeness
echo ""
echo "▶ Event System"
EVENT_COUNT=$(find app/Domain -path "*/Events/*.php" | wc -l | tr -d ' ')
REGISTERED=$(grep -c "::class =>" app/Providers/EventServiceProvider.php 2>/dev/null || echo "0")
if [ "$EVENT_COUNT" -le "$REGISTERED" ]; then
    check_pass "All $EVENT_COUNT events registered in EventServiceProvider"
else
    check_fail "Event count ($EVENT_COUNT) > registered ($REGISTERED)"
fi

# 8. No href="#" in views
echo ""
echo "▶ UI Integrity"
DEAD_LINKS=$(grep -rn 'href="#"' resources/views/ --include="*.blade.php" 2>/dev/null | wc -l | tr -d ' ')
if [ "$DEAD_LINKS" -eq 0 ]; then
    check_pass "No dead links (href=\"#\") in views"
else
    check_warn "$DEAD_LINKS dead links found in views"
fi

# 9. Composer.lock up to date
echo ""
echo "▶ Dependencies"
if [ -f composer.lock ]; then
    check_pass "composer.lock exists"
else
    check_fail "composer.lock missing — run composer install"
fi

if [ -f package-lock.json ] || [ -f package.json ]; then
    check_pass "Node package config exists"
else
    check_warn "No package.json found"
fi

# Summary
echo ""
echo "══════════════════════════════════════════"
echo "  Results: ✓ $PASS passed | ✗ $FAIL failed | ⚠ $WARN warnings"
echo "══════════════════════════════════════════"

if [ "$FAIL" -gt 0 ]; then
    echo ""
    echo "  ❌ DEPLOY BLOCKED — fix failures above"
    exit 1
else
    echo ""
    echo "  ✅ READY TO DEPLOY"
    exit 0
fi
