#!/usr/bin/env bash
# build-dev.sh — Dev build: install deps, start postgres, run migrations
set -euo pipefail

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

echo "=== Installing dependencies ==="
cd "$REPO_DIR"
bun install

echo "=== Starting Postgres ==="
podman compose -f "$REPO_DIR/compose/compose.dev.yml" up -d

echo "=== Waiting for Postgres to be ready ==="
for i in $(seq 1 30); do
    if podman exec splint-postgres pg_isready -U splint -d splint_dev >/dev/null 2>&1; then
        echo "Postgres is ready."
        break
    fi
    echo "  waiting... ($i/30)"
    sleep 1
done

echo "=== Running migrations ==="
cd "$REPO_DIR/packages/server"
bunx drizzle-kit migrate

echo "=== Build (dev) complete ==="
echo "Postgres is running on localhost:5432"
