name: Pre-Release CheapDesign (Preview)

on:
  push:
    branches: [ "dev" ]
    paths:
      - "version.txt"
      - "pack/**"

  workflow_dispatch:

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

    steps:
      - uses: actions/checkout@v4

      # -------- VERSION --------
      - name: Read version
        id: version
        run: |
          VERSION=$(cat version.txt | tr -d '\n')
          PREVIEW_VERSION="${VERSION}-preview"

          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "preview_version=$PREVIEW_VERSION" >> $GITHUB_OUTPUT

      # -------- ZIP BUILD --------
      - name: Build Java texture packs
        run: |
          cd pack
          zip -r ../CheapDesign-${{ steps.version.outputs.preview_version }}.zip .
          cd ..
          cp CheapDesign-${{ steps.version.outputs.preview_version }}.zip CheapMC-${{ steps.version.outputs.preview_version }}.zip

      # -------- GITHUB PRE-RELEASE (OVERWRITE) --------
      - name: Create or update GitHub Pre-Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.version.outputs.preview_version }}
          name: CheapDesign ${{ steps.version.outputs.preview_version }}
          prerelease: true
          allowUpdates: true
          replace_assets: true

          files: |
            CheapDesign-${{ steps.version.outputs.preview_version }}.zip
            CheapMC-${{ steps.version.outputs.preview_version }}.zip
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
