name: Preview Release

on:
  push:
    branches:
      - dev   # Trigger bei Push auf dev-Branch

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      # Schritt 1: Code auschecken
      - name: Checkout Repository
        uses: actions/checkout@v4

      # Schritt 2: Java einrichten (notwendig für Build)
      - name: Set up Java
        uses: actions/setup-java@v4
        with:
          java-version: '21'
          distribution: 'temurin'

      # Schritt 3: Build mit Gradle
      - name: Build Plugin with Gradle
        run: ./gradlew clean build

      # Schritt 4: Preview Release erstellen
      - name: Create Preview Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: preview-${{ github.run_number }}
          name: "Preview Build #${{ github.run_number }}"
          body: |
            🚀 Automatic Preview Build
            Commit: ${{ github.sha }}
            Branch: ${{ github.ref_name }}
          prerelease: true
          files: build/libs/*.jar
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      # Schritt 5: Alte Preview Releases bereinigen (nur 10 neuste behalten)
      - name: Cleanup old Preview Releases
        run: |
          echo "Deleting old Preview Releases, keeping only the 10 newest..."
          gh release list --limit 100 --repo $GITHUB_REPOSITORY \
          | grep preview \
          | sort -rk2 \
          | awk 'NR>10 {print $1}' \
          | xargs -I {} gh release delete {} --repo $GITHUB_REPOSITORY --yes
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
