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

# Fail CI/deploy when a host app is still linked to local Maestro-new.

if [[ -f .maestro-link.json ]]; then
  echo "error: .maestro-link.json found — run php artisan maestro:unlink before deploy" >&2
  exit 1
fi

if [[ -f composer.lock ]] && grep -q '"name": "maestro/core"' composer.lock; then
  if grep -A20 '"name": "maestro/core"' composer.lock | grep -q '"type": "path"'; then
    echo "error: composer.lock installs maestro/core from a path repository" >&2
    exit 1
  fi
fi

if [[ -f package-lock.json ]] && grep -q '"node_modules/@maestro/core"' package-lock.json; then
  if grep -A5 '"node_modules/@maestro/core"' package-lock.json | grep -q '"resolved": "file:'; then
    echo "error: package-lock.json resolves @maestro/core from file:" >&2
    exit 1
  fi
fi

echo "ok: no local Maestro link detected"
