name: Deploy Website

on:
  push:
    branches: [main]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout latest code
        run: |
          if [ -d /tmp/web-projects ]; then
            cd /tmp/web-projects && git pull
          else
            git clone http://localhost:3000/suche-Hermes/web-projects.git /tmp/web-projects
          fi

      - name: Build Astro site
        run: |
          docker run --rm \
            -v /tmp/web-projects:/app \
            -w /app \
            node:22-alpine sh -c "npm install && npm run build"

      - name: Install Nginx config (HTTP)
        run: |
          mkdir -p /etc/nginx/conf.d 2>/dev/null
          cp /tmp/web-projects/nginx/longde.cloud.conf /etc/nginx/conf.d/longde.cloud.conf
          mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled 2>/dev/null
          cp /tmp/web-projects/nginx/longde.cloud.conf /etc/nginx/sites-available/longde.cloud.conf 2>/dev/null
          ln -sf /etc/nginx/sites-available/longde.cloud.conf /etc/nginx/sites-enabled/longde.cloud.conf 2>/dev/null

      - name: Deploy to web root
        run: |
          rm -rf /var/www/longde.cloud/*
          cp -r /tmp/web-projects/dist/* /var/www/longde.cloud/

      - name: Resolve stuck Prisma migrations
        run: |
          set -x
          MYSQL_PASS=$(docker exec mysql-zhixi printenv MYSQL_PASSWORD 2>/dev/null || echo "Zhixi@2026!App")
          MYSQL_EXEC="docker exec mysql-zhixi mysql -u zhixi_user -p'$MYSQL_PASS' zhixi"

          echo "=== Checking for failed migrations ==="
          $MYSQL_EXEC -e "SELECT migration_name, applied_steps_count, rolled_back_at, SUBSTRING(logs, 1, 120) as logs_preview FROM _prisma_migrations ORDER BY started_at DESC LIMIT 5;" 2>/dev/null || echo "(could not query _prisma_migrations)"

          # Check for failed/stuck migrations
          FAILED=$($MYSQL_EXEC -N -e "SELECT migration_name FROM _prisma_migrations WHERE logs LIKE '%failed%' LIMIT 1;" 2>/dev/null || true)

          if [ -n "$FAILED" ]; then
            echo "[deploy] Found failed migration: $FAILED, cleaning up..."

            # Check what was partially created
            echo "=== Checking partial state ==="
            $MYSQL_EXEC -e "SELECT COUNT(*) AS cnt FROM information_schema.TABLES WHERE TABLE_SCHEMA='zhixi' AND TABLE_NAME='AiUsageLog';" 2>/dev/null || true
            $MYSQL_EXEC -e "SELECT COUNT(*) AS cnt FROM information_schema.TABLES WHERE TABLE_SCHEMA='zhixi' AND TABLE_NAME='WaitlistEntry';" 2>/dev/null || true
            $MYSQL_EXEC -e "SELECT COUNT(*) AS cnt FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='zhixi' AND TABLE_NAME='UploadedFile' AND COLUMN_NAME='objectKey';" 2>/dev/null || true

            # Drop partially-created objects
            $MYSQL_EXEC -e "DROP TABLE IF EXISTS AiUsageLog;" 2>/dev/null || true
            $MYSQL_EXEC -e "DROP TABLE IF EXISTS WaitlistEntry;" 2>/dev/null || true
            $MYSQL_EXEC -e "ALTER TABLE UploadedFile DROP COLUMN IF EXISTS objectKey;" 2>/dev/null || true
            $MYSQL_EXEC -e "ALTER TABLE UploadedFile DROP COLUMN IF EXISTS bucket;" 2>/dev/null || true
            $MYSQL_EXEC -e "DROP INDEX UploadedFile_objectKey_idx ON UploadedFile;" 2>/dev/null || true

            # Remove the failed migration record
            $MYSQL_EXEC -e "DELETE FROM _prisma_migrations WHERE migration_name = '$FAILED';"
            echo "[deploy] Deleted failed migration record: $FAILED"
          else
            echo "[deploy] No failed migrations found"
          fi

          set +x

      - name: Ensure API backend is running
        run: |
          set -x

          # Clone or pull api-server repo for docker-compose.yml
          if [ -d /tmp/api-server ]; then
            cd /tmp/api-server && git pull 2>/dev/null || true
          else
            git clone http://localhost:3000/suche-Hermes/api-server.git /tmp/api-server 2>/dev/null || true
          fi

          # Create shared network if missing
          docker network inspect zhixi-net >/dev/null 2>&1 || docker network create zhixi-net

          # Start MySQL + Redis via docker compose
          if [ -f /tmp/api-server/docker-compose.yml ]; then
            cd /tmp/api-server && docker compose up -d mysql redis 2>&1 || true
          fi

          # Wait for MySQL to be ready
          sleep 5

          # Check current state
          echo "=== Container status ==="
          docker ps -a --format 'table {{.Names}}\t{{.Status}}' 2>/dev/null | grep -iE 'zhixi|mysql|redis' || true

          # If API container exists but not running (stopped or restarting), handle it
          if docker ps -a --format '{{.Names}}' | grep -q '^zhixi-api$'; then
            STATUS=$(docker ps -a --format '{{.Status}}' --filter name=zhixi-api)
            if echo "$STATUS" | grep -q '^Up'; then
              echo "[deploy] zhixi-api is already running"
            else
              echo "[deploy] zhixi-api status: $STATUS — stopping and recreating..."
              docker stop zhixi-api 2>/dev/null || true
              docker rm zhixi-api 2>/dev/null || true
              # Rebuild image to pick up migration fixes
              cd /tmp/api-server && docker build -t zhixi-api:latest . 2>&1 || true
              ENV_FILE=""
              [ -f /etc/zhixi/.env.production ] && ENV_FILE="--env-file /etc/zhixi/.env.production"
              docker run -d \
                --name zhixi-api \
                --network zhixi-net \
                --restart unless-stopped \
                -p 3001:3000 \
                $ENV_FILE \
                zhixi-api:latest 2>&1 || true
              sleep 8
            fi
          else
            # No container — create one
            echo "[deploy] zhixi-api not found, building and creating..."
            cd /tmp/api-server && docker build -t zhixi-api:latest . 2>&1 || true
            ENV_FILE=""
            [ -f /etc/zhixi/.env.production ] && ENV_FILE="--env-file /etc/zhixi/.env.production"
            docker run -d \
              --name zhixi-api \
              --network zhixi-net \
              --restart unless-stopped \
              -p 3001:3000 \
              $ENV_FILE \
              zhixi-api:latest 2>&1 || true
            sleep 8
          fi

          # Health check
          if curl -sf http://localhost:3001/health; then
            echo "[deploy] Backend health OK"
          else
            echo "[deploy] WARNING: Backend health check failed"
            echo "=== API container logs (tail 40) ==="
            docker logs zhixi-api --tail 40 2>&1 || true
          fi

          set +x

      - name: Install Nginx config (HTTPS)
        run: |
          if [ -f /etc/letsencrypt/live/longde.cloud/fullchain.pem ]; then
            cp /tmp/web-projects/nginx/longde.cloud-ssl.conf /etc/nginx/conf.d/longde.cloud-ssl.conf
          else
            echo "[deploy] No SSL cert found, skipping HTTPS config"
            rm -f /etc/nginx/conf.d/longde.cloud-ssl.conf
          fi

      - name: Dump debug info
        run: |
          {
            echo "=== Deploy $(date) ==="
            echo ""
            echo "=== _prisma_migrations ==="
            MYSQL_PASS=$(docker exec mysql-zhixi printenv MYSQL_PASSWORD 2>/dev/null || echo "Zhixi@2026!App")
            docker exec mysql-zhixi mysql -u zhixi_user -p"$MYSQL_PASS" zhixi -e "SELECT migration_name, applied_steps_count, rolled_back_at, SUBSTRING(logs, 1, 200) as logs_preview FROM _prisma_migrations ORDER BY started_at DESC LIMIT 5;" 2>&1 || echo "(could not query)"
            echo ""
            echo "=== docker ps -a ==="
            docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}' 2>&1
            echo ""
            echo "=== zhixi-api logs (tail 50) ==="
            docker logs zhixi-api --tail 50 2>&1 || echo "(no logs)"
            echo ""
            echo "=== docker network ls ==="
            docker network ls 2>&1
            echo ""
            echo "=== port 3001 ==="
            ss -tlnp | grep 3001 2>/dev/null || netstat -tlnp 2>/dev/null | grep 3001 || echo "(no listener on 3001)"
            echo ""
            echo "=== nginx -t ==="
            nginx -t 2>&1
          } > /var/www/longde.cloud/deploy-status.txt 2>&1

      - name: Reload Nginx
        run: nginx -s reload || nginx -t 2>&1
