name: Deploy

# Один пайплайн, две джобы (по аналогии с netrunner-backend):
#  - deploy-dev:  авто, срабатывает после успешного "Build & Push Image" на main.
#  - deploy-prod: вручную из вкладки Actions ("Run workflow").
on:
  workflow_run:
    workflows: ["Build & Push Image"]
    types: [completed]
    branches: [main]
  workflow_dispatch:

jobs:
  deploy-dev:
    name: Auto-deploy to Dev VPS
    if: github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.workflow_run.head_sha }}

      # Копируем только compose-файл — сам образ дев-сервер тянет из GHCR.
      - name: Sync compose file to Dev VPS
        uses: appleboy/scp-action@v0.1.7
        with:
          host: ${{ vars.DEV_VPS_IP }}
          username: root
          key: ${{ secrets.DEV_SSH_PRIVATE_KEY }}
          source: "docker-compose.dev-remote.yml"
          target: "/root/netrunner-frontend-dev"
          strip_components: 0

      - name: Write .env and deploy via SSH
        uses: appleboy/ssh-action@v1
        with:
          host: ${{ vars.DEV_VPS_IP }}
          username: root
          key: ${{ secrets.DEV_SSH_PRIVATE_KEY }}
          envs: "BOT_USERNAME"
          script: |
            set -e
            cd /root/netrunner-frontend-dev

            # Единственное, что этому compose-файлу нужно из .env — свой dev-бот
            # (см. BOT_USERNAME:? в docker-compose.dev-remote.yml). Тот же бот,
            # что и у netrunner-backend dev — vars.DEV_BOT_USERNAME там же.
            cat > .env <<EOF
            BOT_USERNAME=${BOT_USERNAME}
            EOF
            chmod 600 .env

            echo "🚀 Deploying landing+proxy:latest to DEV"
            echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin

            docker compose -f docker-compose.dev-remote.yml pull
            docker compose -f docker-compose.dev-remote.yml up -d --remove-orphans

            docker image prune -f

            echo "✅ Dev synchronized."
        env:
          BOT_USERNAME: ${{ vars.DEV_BOT_USERNAME }}

  deploy-prod:
    name: Pull & restart on Prod VPS
    if: github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Copy docker-compose.yml to VPS
        uses: appleboy/scp-action@v0.1.7
        with:
          host: ${{ vars.VPS_IP }}
          username: root
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          source: "docker-compose.yml"
          target: "/root/netrunner-frontend"

      - name: Deploy via SSH
        uses: appleboy/ssh-action@v1.0.3
        with:
          host: ${{ vars.VPS_IP }}
          username: root
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            set -e
            cd /root/netrunner-frontend
            echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin

            echo "🚀 Deploying landing+proxy:latest to PROD"

            # Тянем оба образа
            docker compose pull

            # Поднимаем всё
            docker compose up -d --force-recreate --remove-orphans

            # Чистим старье
            docker image prune -f

            echo "✅ Uplink synchronized."
