#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
PLUGIN_DIR="${REPO_ROOT}/feca_mailshots_plugin"
ASSET_DIR="${PLUGIN_DIR}/assets/vendor"

INSTALL_PHP=1
INSTALL_EDITOR_ASSETS=1
INSTALL_NODE=1

usage() {
  cat <<'USAGE'
Usage: scripts/install_dependencies.sh [--skip-php] [--skip-editor-assets] [--skip-node]

Installs third-party dependencies used by the plugin:
  - PHP runtime deps (Twig, Dompdf) via Composer
  - Bundled editor assets (Jodit + Ace) under feca_mailshots_plugin/assets/vendor
  - Node test deps via npm ci (Playwright package only; browsers are not installed here)
USAGE
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --skip-php)
      INSTALL_PHP=0
      shift
      ;;
    --skip-editor-assets)
      INSTALL_EDITOR_ASSETS=0
      shift
      ;;
    --skip-node)
      INSTALL_NODE=0
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "Unknown argument: $1" >&2
      usage >&2
      exit 1
      ;;
  esac
done

if [[ ! -d "${PLUGIN_DIR}" ]]; then
  echo "Error: plugin directory not found: ${PLUGIN_DIR}" >&2
  exit 1
fi

if [[ "${INSTALL_PHP}" -eq 1 ]]; then
  if command -v composer >/dev/null 2>&1; then
    echo "Installing PHP dependencies via Composer..."
    (
      cd "${REPO_ROOT}"
      composer install --no-dev --prefer-dist --no-interaction --optimize-autoloader
    )
  elif [[ -f "${REPO_ROOT}/vendor/autoload.php" ]]; then
    echo "Warning: composer not found; using existing ${REPO_ROOT}/vendor."
  else
    echo "Error: composer is required (or provide prebuilt vendor/autoload.php)." >&2
    exit 1
  fi
fi

if [[ "${INSTALL_EDITOR_ASSETS}" -eq 1 ]]; then
  if ! command -v curl >/dev/null 2>&1; then
    echo "Error: curl is required to download editor assets." >&2
    exit 1
  fi

  echo "Installing bundled editor assets (Jodit + Ace)..."
  mkdir -p "${ASSET_DIR}/jodit" "${ASSET_DIR}/ace"

  download() {
    local url="$1"
    local dest="$2"
    curl -fsSL "${url}" -o "${dest}"
  }

  download "https://cdn.jsdelivr.net/npm/jodit@4.7.9/es2021/jodit.min.css" "${ASSET_DIR}/jodit/jodit.min.css"
  download "https://cdn.jsdelivr.net/npm/jodit@4.7.9/es2021/jodit.min.js" "${ASSET_DIR}/jodit/jodit.min.js"

  download "https://cdn.jsdelivr.net/npm/ace-builds@1.36.0/src-min-noconflict/ace.js" "${ASSET_DIR}/ace/ace.js"
  download "https://cdn.jsdelivr.net/npm/ace-builds@1.36.0/src-min-noconflict/mode-html.js" "${ASSET_DIR}/ace/mode-html.js"
  download "https://cdn.jsdelivr.net/npm/ace-builds@1.36.0/src-min-noconflict/worker-html.js" "${ASSET_DIR}/ace/worker-html.js"
  download "https://cdn.jsdelivr.net/npm/ace-builds@1.36.0/src-min-noconflict/ext-language_tools.js" "${ASSET_DIR}/ace/ext-language_tools.js"
  download "https://cdn.jsdelivr.net/npm/ace-builds@1.36.0/src-min-noconflict/theme-textmate.js" "${ASSET_DIR}/ace/theme-textmate.js"
fi

if [[ "${INSTALL_NODE}" -eq 1 && -f "${REPO_ROOT}/package-lock.json" ]]; then
  if ! command -v npm >/dev/null 2>&1; then
    echo "Error: npm is required to install Node dependencies." >&2
    exit 1
  fi
  echo "Installing Node dependencies via npm ci..."
  (
    cd "${REPO_ROOT}"
    npm ci
  )
fi

echo "Dependency installation complete."
