#!/usr/bin/env bash
# Post-deploy on cPanel (after git pull or .cpanel.yml rsync)
set -euo pipefail

PHP_BIN="${PHP_BIN:-php}"
COMPOSER_BIN="${COMPOSER_BIN:-composer}"
RUN_COMPOSER="${RUN_COMPOSER:-false}"
# On shared cPanel, artisan down often leaves the site stuck on 503 if migrate/cache fails.
USE_MAINTENANCE="${USE_MAINTENANCE:-false}"
MAINTENANCE_ENABLED=0

cleanup() {
  if [[ "${MAINTENANCE_ENABLED}" -eq 1 ]]; then
    "${PHP_BIN}" artisan up 2>/dev/null || rm -f storage/framework/down storage/framework/maintenance.php
    echo "== Maintenance mode disabled =="
  fi
}
trap cleanup EXIT

echo "== cPanel post-deploy =="
echo "PHP: ${PHP_BIN}"

mkdir -p \
  storage/app/public \
  storage/framework/cache/data \
  storage/framework/sessions \
  storage/framework/views \
  storage/logs \
  bootstrap/cache

fix_permissions() {
  chmod 711 ~ 2>/dev/null || true
  chmod 755 ~/public_html 2>/dev/null || true
  chmod 755 ~/public_html/test.duiclick.com 2>/dev/null || true
  chmod 755 ~/public_html/test.duiclick.com/public 2>/dev/null || true
  chmod -R ug+rwx storage bootstrap/cache 2>/dev/null || true
  chmod -R 777 storage bootstrap/cache 2>/dev/null || true
}

fix_permissions

if [[ ! -f .env ]]; then
  echo "ERROR: .env is missing. Copy .env.example to .env in cPanel File Manager first."
  exit 1
fi

if [[ "${RUN_COMPOSER}" == "true" ]]; then
  if command -v "${COMPOSER_BIN}" >/dev/null 2>&1; then
    "${COMPOSER_BIN}" install --no-dev --prefer-dist --no-interaction --optimize-autoloader
  elif [[ -f composer.phar ]]; then
    "${PHP_BIN}" composer.phar install --no-dev --prefer-dist --no-interaction --optimize-autoloader
  else
    echo "WARN: Composer not found; skipping (vendor/ is included in this repo)."
  fi
else
  if [[ -f composer.json ]]; then
    if command -v "${COMPOSER_BIN}" >/dev/null 2>&1; then
      "${COMPOSER_BIN}" dump-autoload --no-interaction --optimize 2>/dev/null || true
    elif [[ -f composer.phar ]]; then
      "${PHP_BIN}" composer.phar dump-autoload --no-interaction --optimize 2>/dev/null || true
    fi
  fi
fi

"${PHP_BIN}" artisan storage:link 2>/dev/null || true

if [[ "${USE_MAINTENANCE}" == "true" ]]; then
  "${PHP_BIN}" artisan down --retry=30 2>/dev/null || true
  MAINTENANCE_ENABLED=1
fi

"${PHP_BIN}" artisan migrate --force
"${PHP_BIN}" artisan config:clear
"${PHP_BIN}" artisan route:clear
"${PHP_BIN}" artisan view:clear
"${PHP_BIN}" artisan cache:clear
rm -f bootstrap/cache/config.php bootstrap/cache/routes-v7.php bootstrap/cache/packages.php bootstrap/cache/services.php 2>/dev/null || true

# Do NOT config:cache on cPanel — stale/broken cache causes 500/503 after deploy.
# Routes load fresh from routes/web.php (see project deploy docs).

if [[ "${USE_MAINTENANCE}" == "true" ]]; then
  MAINTENANCE_ENABLED=0
  "${PHP_BIN}" artisan up 2>/dev/null || rm -f storage/framework/down storage/framework/maintenance.php
fi

# Always clear maintenance flags in case a previous deploy failed mid-script.
rm -f storage/framework/down storage/framework/maintenance.php 2>/dev/null || true
"${PHP_BIN}" artisan up 2>/dev/null || true

echo ""
echo "IMPORTANT: In cPanel go to MultiPHP Manager -> click 'Reset OPcache' for this domain,"
echo "or restart PHP-FPM/LiteSpeed, then hard-refresh the browser (Ctrl+F5)."

fix_permissions

if ! grep -q "Opening Balance" resources/views/layouts/app.blade.php 2>/dev/null; then
  echo "WARN: Opening Balance menu not found in app layout — git pull may be incomplete."
fi

if ! grep -q "opening-balance" routes/web.php 2>/dev/null; then
  echo "WARN: opening-balance route not found — run git pull origin main on this server."
fi

echo "== cPanel post-deploy complete =="
