#!/bin/sh
# Don't exit on error during migrations
set +e

# Add node_modules/.bin to PATH
export PATH="/app/node_modules/.bin:$PATH"

echo "🔄 Running database migrations..."
prisma migrate deploy
MIGRATE_EXIT=$?

if [ $MIGRATE_EXIT -ne 0 ]; then
  echo "⚠️  Migration failed with exit code $MIGRATE_EXIT. Checking if schema needs reset..."
  echo "📋 Listing migration directory contents:"
  ls -laR prisma/migrations/ || echo "Cannot list migrations"
fi

echo "🌱 Seeding database (will skip if already seeded)..."
prisma db seed || echo "⚠️  Seeding skipped or already completed"

echo "✅ Starting application despite any migration issues..."
echo "🚀 Starting Next.js application..."

# Always start the app
exec node server.js
