name: Deploy Iddaai Backend

on:
  push:
    branches: [main]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Kodu Cek
        uses: actions/checkout@v4

      - name: Docker Build
        run: docker build -t iddaai-be:latest .

      - name: Eski Konteyneri Sil
        run: docker rm -f iddaai-be || true

      - name: Yeni Versiyonu Baslat
        run: |
          docker run -d \
            --name iddaai-be \
            --restart unless-stopped \
            --network iddaai_iddaai-network \
            -p 127.0.0.1:1810:3005 \
            -e NODE_ENV=production \
            -e DATABASE_URL='postgresql://iddaai_user:IddaA1_S4crET!@iddaai-postgres:5432/iddaai_db?schema=public' \
            -e REDIS_HOST='iddaai-redis' \
            -e REDIS_PORT='6379' \
            -e REDIS_PASSWORD='IddaA1_Redis_Pass!' \
            -e AI_ENGINE_URL='http://iddaai-ai-engine:8000' \
            -e JWT_SECRET='b7V8jM2wP1L5mQxs2RdfFkAsLpI2oG!w' \
            -e JWT_ACCESS_EXPIRATION='1d' \
            iddaai-be:latest /bin/sh -c "npx prisma migrate deploy && node dist/src/main.js"
