name: Create GitHub Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

jobs:
  # Windows executable build temporarily disabled. To re-enable:
  # 1. Uncomment the 'build-windows' job below.
  # 2. Add 'needs: build-windows' back to the 'release' job.
  # 3. Re-add the artifact download & file attachment steps.
  # Reason: Current releases do not ship a Windows EXE; focusing on container / source distribution.
  #
  # build-windows:
  #   name: Build Windows EXE
  #   runs-on: windows-latest
  #   steps:
  #     - name: Checkout
  #       uses: actions/checkout@v5.0.0
  #
  #     - name: Setup Python
  #       uses: actions/setup-python@v5.6.0
  #       with:
  #         python-version: '3.11'
  #
  #     - name: Install dependencies
  #       shell: powershell
  #       run: |
  #         python -m pip install --upgrade pip wheel setuptools
  #         if (Test-Path 'requirements.txt') { pip install -r requirements.txt }
  #         pip install pyinstaller
  #
  #     - name: Build executable (PyInstaller)
  #       shell: powershell
  #       run: |
  #         pyinstaller mtg_deckbuilder.spec
  #         if (!(Test-Path dist/mtg-deckbuilder.exe)) {
  #           Write-Host 'Spec build failed; retrying simple build with --paths code'
  #           pyinstaller --onefile --name mtg-deckbuilder --paths code code/main.py
  #         }
  #         if (!(Test-Path dist/mtg-deckbuilder.exe)) { throw 'Build failed: dist/mtg-deckbuilder.exe not found' }
  #
  #     - name: Upload artifact (Windows EXE)
  #       uses: actions/upload-artifact@v4.6.2
  #       with:
  #         name: mtg-deckbuilder-windows
  #         path: dist/mtg-deckbuilder.exe

  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v5.0.0

      - name: Prepare release notes
        id: notes
        shell: bash
        run: |
          VERSION_REF="${GITHUB_REF##*/}"  # e.g. v1.2.3
          VERSION_NO_V="${VERSION_REF#v}"
          VERSIONED_NOTES="docs/releases/${VERSION_REF}.md"
          TEMPLATE="RELEASE_NOTES_TEMPLATE.md"
          
          # Prefer versioned release notes file if it exists
          if [ -f "$VERSIONED_NOTES" ]; then
            cp "$VERSIONED_NOTES" RELEASE_NOTES.md
          elif [ -f "$TEMPLATE" ]; then
            sed "s/\${VERSION}/${VERSION_REF}/g" "$TEMPLATE" > RELEASE_NOTES.md
          else
            echo "# MTG Python Deckbuilder ${VERSION_REF}" > RELEASE_NOTES.md
            echo >> RELEASE_NOTES.md
            echo "Automated release." >> RELEASE_NOTES.md
          fi
          echo "version=$VERSION_REF" >> $GITHUB_OUTPUT
          echo "notes_file=RELEASE_NOTES.md" >> $GITHUB_OUTPUT

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2.3.2
        with:
          tag_name: ${{ steps.notes.outputs.version }}
          name: ${{ steps.notes.outputs.version }}
          body_path: ${{ steps.notes.outputs.notes_file }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
