#!/bin/bash

# Server Development Mode
# Auto-recompiles and restarts the server on file changes

set -e

echo "🚀 Server Development Mode"
echo "================================="

# Check if database is ready
echo "📡 Checking database connection..."
while ! pg_isready -h db -p 5432 -q; do
    echo "⏳ Waiting for PostgreSQL..."
    sleep 1
done

# Run migrations
echo "📋 Running database migrations..."
sqlx migrate run

echo "👀 Starting file watcher..."
echo "💡 Tip: Press Ctrl+C to stop"
echo ""

cargo watch \
  --clear \
  --watch src \
  --watch Cargo.toml \
  --watch migrations \
  --exec "run --bin server" \
  --shell 'echo "🔄 Recompiling..."'
