name: Deploy Addons

on:
  push:
    branches: [main]
    paths:
      - 'addon/**'
      - 'lobby-addon/**'
      - 'hub-return-addon/**'
      - 'village-evolution-addon/**'
      - 'monkey-addon/**'
      - 'private-chest-addon/**'
      - 'home-sign-addon/**'
      - 'keep-inventory-addon/**'
      - 'postal-service-addon/**'
      - 'camping-supplies-addon/**'
      - 'docker-compose.yml'
      - 'scripts/**'

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Deploy via SSH
        uses: appleboy/ssh-action@v1
        with:
          host: ${{ secrets.DEPLOY_HOST }}
          username: ${{ secrets.DEPLOY_USER }}
          password: ${{ secrets.DEPLOY_PASSWORD }}
          port: 22
          script: |
            set -e
            APP_DIR="$HOME/minecraft-multiworld"
            PATHS="addon/ lobby-addon/ hub-return-addon/ village-evolution-addon/ monkey-addon/ private-chest-addon/ home-sign-addon/ keep-inventory-addon/ postal-service-addon/ camping-supplies-addon/ docker-compose.yml"

            # First run: clone. Subsequent: pull.
            if [ ! -d "$APP_DIR/.git" ]; then
              cd "$APP_DIR"
              git init
              git remote add origin https://git.silverlabs.uk/SilverLABS/minecraft-aiworld.git
              git fetch origin main
              git checkout -f origin/main -- $PATHS
            else
              cd "$APP_DIR"
              git fetch origin main
              git checkout -f origin/main -- $PATHS
            fi

            # Recreate containers so any new docker-compose volume mounts are applied,
            # then restart remaining containers to pick up script changes
            docker compose up -d --force-recreate

            # Wait and verify
            sleep 10
            docker ps --filter "name=mc-" --format "{{.Names}}: {{.Status}}"
