#!/usr/bin/env bash
# ProInvoice — pre-production verification (run from project root).
# Usage: ./scripts/production-verify.sh
# Optional: SKIP_NPM=1  SKIP_COMPOSER_DEV=0  RUN_OPTIMIZE=1

set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

echo "==> ProInvoice production verification"
echo "    Root: $ROOT"
echo ""

if [[ ! -f .env ]]; then
  echo "ERROR: .env missing. Copy .env.example to .env and configure." >&2
  exit 1
fi

# Warn if debug is on (read without sourcing)
if grep -q '^APP_DEBUG=true' .env 2>/dev/null; then
  echo "WARNING: APP_DEBUG=true — set APP_DEBUG=false for production." >&2
fi

echo "==> Composer validate"
composer validate --no-check-publish

echo "==> PHP / Laravel"
php artisan about

if [[ "${SKIP_NPM:-}" != "1" ]]; then
  if command -v npm >/dev/null 2>&1; then
    echo "==> npm ci && npm run build"
    npm ci
    npm run build
  else
    echo "SKIP: npm not installed (set assets elsewhere or install Node)."
  fi
else
  echo "SKIP: npm (SKIP_NPM=1)"
fi

echo "==> PHPUnit"
php artisan test

if [[ "${RUN_OPTIMIZE:-}" == "1" ]]; then
  echo "==> Laravel caches (production:optimize)"
  composer run production:optimize
  echo "NOTE: Run composer run production:clear when returning to local dev."
else
  echo "SKIP: caches (set RUN_OPTIMIZE=1 to run config/route/view/event cache)."
fi

echo ""
echo "✓ Verification finished."
echo "  Deploy checklist: doc/PRODUCTION_READINESS.md"
