#!/bin/sh
set -e

echo "🔄 Running database migrations..."
# Check if migrations directory exists and has files
if [ -d "prisma/migrations" ] && [ "$(ls -A prisma/migrations 2>/dev/null)" ]; then
  echo "Found migration files, running migrate deploy..."
  node_modules/.bin/prisma migrate deploy
else
  echo "No migration files found, running db push..."
  node_modules/.bin/prisma db push --accept-data-loss
fi

echo "🌱 Seeding database (if needed)..."
node_modules/.bin/tsx prisma/seed.ts || echo "⚠️  Seed failed or already completed, continuing..."

echo "🚀 Starting application..."
exec node server.js
