#!/bin/sh
set -e

echo "=========================================="
echo "iConsulting Database Initialization"
echo "=========================================="

# Wait for database to be ready
echo "Waiting for database..."
until pg_isready -h ${DB_HOST:-postgres} -p ${DB_PORT:-5432} -U ${DB_USER:-postgres} 2>/dev/null; do
  echo "Database is unavailable - sleeping"
  sleep 2
done
echo "Database is ready!"

# Run seed script
echo "Running seed script..."
cd /app/scripts
npx ts-node seed.ts

echo "=========================================="
echo "Database initialization completed!"
echo "=========================================="

# Execute the main command
exec "$@"
