#!/bin/sh
set -e

cd "$(dirname "$0")/.."

# The pad template's node_modules lacks this app's extra packages (router, Tailwind); a no-op once installed.
npm install --no-audit --no-fund

# Locally the deps live in the mise venv; the pad's system python3 has Flask and psycopg2 preinstalled.
PYTHON=python3
[ -x .venv/bin/python ] && PYTHON=.venv/bin/python

"$PYTHON" backend/app.py &
FLASK_PID=$!

until curl -s -o /dev/null http://localhost:5000/api/health; do
  if ! kill -0 "$FLASK_PID" 2>/dev/null; then
    echo "Flask exited during startup; fix the error above (often a missing dependency or an unreachable PostgreSQL) and restart." >&2
    exit 1
  fi
  sleep 0.5
done

exec node_modules/.bin/vite
