name: Build Mission PBO

on:
  push:
    branches: [main, master]
    tags: ["v*"]
  workflow_dispatch:

env:
  PBO_NAME: "KPLib-Ruha.ruha.pbo"

jobs:
  pack:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Pack mission folder into PBO
        run: |
          docker run --name packer \
            -v "${{ github.workspace }}:/workspace:rw" \
            -w /workspace \
            arwynfr/armake2:debian \
            pack /workspace "${{ env.PBO_NAME }}"

          docker cp packer:/workspace/${{ env.PBO_NAME }} .

      - name: Cleanup Packer Container
        # if: always() garantisce l'esecuzione anche se i passaggi sopra falliscono
        if: always()
        run: |
          docker rm -f packer || true

      - name: Verify PBO integrity
        run: |
          docker run --rm \
            -v "${{ github.workspace }}:/workspace" \
            -w /workspace \
            arwynfr/armake2:debian \
            inspect "./${{ env.PBO_NAME }}"

      - name: Upload PBO as artifact
        uses: https://github.com/christopherHX/gitea-upload-artifact@v4
        with:
          name: ${{ env.PBO_NAME }}
          path: ${{ env.PBO_NAME }}
          if-no-files-found: error

  deploy:
    if: false
    needs: [pack]
    runs-on: ubuntu-latest
    steps:
      - name: Download PBO artifact
        uses: actions/download-artifact@v4
        with:
          name: ${{ env.PBO_NAME }}

      - name: Deploy to game server via SCP
        uses: appleboy/scp-action@v1
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USER }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          source: ${{ env.PBO_NAME }}
          target: /path/to/server/mpmissions/
