name: Build and Release

on:
  push:
    tags:
      - 'v*'

jobs:
  check-tag:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - id: set-matrix
        run: |
          TAG=${GITHUB_REF_NAME}
          if [[ "$TAG" == *"-linux" ]]; then
            echo 'matrix={"include":[{"os":"ubuntu-latest","artifact_name":"disco-reaper-linux","asset_name":"disco-reaper-linux.zip","executable_path":"dist/DiscoReaper"}]}' >> $GITHUB_OUTPUT
          elif [[ "$TAG" == *"-windows" ]]; then
            echo 'matrix={"include":[{"os":"windows-latest","artifact_name":"disco-reaper-windows","asset_name":"disco-reaper-windows.zip","executable_path":"dist/DiscoReaper.exe"}]}' >> $GITHUB_OUTPUT
          elif [[ "$TAG" == *"-mac" ]]; then
            echo 'matrix={"include":[{"os":"macos-latest","artifact_name":"disco-reaper-macos","asset_name":"disco-reaper-macos.zip","executable_path":"dist/DiscoReaper"}]}' >> $GITHUB_OUTPUT
          else
            echo 'matrix={"include":[{"os":"ubuntu-latest","artifact_name":"disco-reaper-linux","asset_name":"disco-reaper-linux.zip","executable_path":"dist/DiscoReaper"},{"os":"windows-latest","artifact_name":"disco-reaper-windows","asset_name":"disco-reaper-windows.zip","executable_path":"dist/DiscoReaper.exe"},{"os":"macos-latest","artifact_name":"disco-reaper-macos","asset_name":"disco-reaper-macos.zip","executable_path":"dist/DiscoReaper"}]}' >> $GITHUB_OUTPUT
          fi

  build:
    needs: check-tag
    runs-on: ${{ matrix.os }}
    strategy:
      matrix: ${{ fromJson(needs.check-tag.outputs.matrix) }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install pyinstaller Pillow

      - name: Bake Version
        shell: bash
        run: |
          GIT_VERSION=${GITHUB_REF_NAME:-$(git describe --tags --abbrev=0 2>/dev/null || echo "Unknown")}
          echo "Baking version: $GIT_VERSION"
          echo "__version__ = \"$GIT_VERSION\"" > src/core/_baked_version.py

      - name: Build with PyInstaller
        run: |
          pyinstaller disco-reaper.spec

      - name: Clean up Baked Version
        shell: bash
        run: rm -f src/core/_baked_version.py

      - name: Prepare Release Asset (Linux/MacOS)
        if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest'
        run: |
          mkdir REAPER
          cp ${{ matrix.executable_path }} REAPER/DiscoReaper
          chmod +x REAPER/DiscoReaper
          
          # Create launcher for Linux/Mac
          cat << 'EOF' > REAPER/Launch-Reaper.sh
          #!/bin/bash
          BASEDIR=$(dirname "$0")
          cd "$BASEDIR"
          if [ -f "./DiscoReaper" ]; then
              ./DiscoReaper
          else
              echo "Error: DiscoReaper binary not found."
              read -p "Press enter to exit..."
          fi
          EOF
          chmod +x REAPER/Launch-Reaper.sh
          
          zip -r ${{ matrix.asset_name }} REAPER

      - name: Prepare Release Asset (Windows)
        if: matrix.os == 'windows-latest'
        shell: pwsh
        run: |
          New-Item -ItemType Directory -Path "REAPER" -Force
          Copy-Item "${{ matrix.executable_path }}" -Destination "REAPER\DiscoReaper.exe"
          Compress-Archive -Path "REAPER" -DestinationPath "${{ matrix.asset_name }}" -Force

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact_name }}
          path: ${{ matrix.asset_name }}

  release:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download Artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: artifacts/**/*.zip
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
