name: CLI Release

on:
  push:
    tags:
      - 'cli/v*'

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-go@v5
        with:
          go-version: '1.21'

      - name: Build binaries
        working-directory: cli
        run: |
          VERSION="${{ github.ref_name }}"
          VERSION="${VERSION#cli/v}"
          mkdir -p ../dist
          GOOS=linux   GOARCH=amd64 go build -ldflags="-s -w -X main.Version=${VERSION}" -o ../dist/msdl-linux-amd64   .
          GOOS=linux   GOARCH=arm64 go build -ldflags="-s -w -X main.Version=${VERSION}" -o ../dist/msdl-linux-arm64   .
          GOOS=darwin  GOARCH=amd64 go build -ldflags="-s -w -X main.Version=${VERSION}" -o ../dist/msdl-darwin-amd64  .
          GOOS=darwin  GOARCH=arm64 go build -ldflags="-s -w -X main.Version=${VERSION}" -o ../dist/msdl-darwin-arm64  .
          GOOS=windows GOARCH=amd64 go build -ldflags="-s -w -X main.Version=${VERSION}" -o ../dist/msdl-windows-amd64.exe .

      - name: Generate changelog
        id: changelog
        run: |
          PREV_TAG=$(git tag --sort=-version:refname | grep 'cli/v' | sed -n '2p')
          if [ -n "$PREV_TAG" ]; then
            CHANGELOG=$(git log --pretty=format:"- %s" "$PREV_TAG".."${{ github.ref_name }}" -- cli/ backend/ frontend/ .github/workflows/cli-release.yml | grep -v '^- chore(winget)' | grep -v '^- chore(release): bump' | head -30)
            if [ -z "$CHANGELOG" ]; then
              CHANGELOG="- Maintenance release (see commit history for details)"
            fi
          else
            CHANGELOG="- Initial release"
          fi
          echo "changelog<<EOF" >> $GITHUB_OUTPUT
          echo "$CHANGELOG" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.ref_name }}
          name: "msdl CLI ${{ github.ref_name }}"
          files: dist/*
          body: |
            ## What's New

            ${{ steps.changelog.outputs.changelog }}

            ## Install

            **Windows (winget):**
            ```
            winget install starkSV.msdl
            ```

            **Windows (direct download):** Download `msdl-windows-amd64.exe` below, rename to `msdl.exe`, place in a folder on your PATH.

            **macOS/Linux (Homebrew):**
            ```bash
            brew tap starkSV/msdl
            brew install msdl-cli
            ```
            (formula is `msdl-cli`, not `msdl` -- homebrew/core has an unrelated package named `msdl`; the installed command is still just `msdl`)

            **Arch Linux (AUR):**
            ```bash
            yay -S msdl-bin
            # or: paru -S msdl-bin
            ```
            [aur.archlinux.org/packages/msdl-bin](https://aur.archlinux.org/packages/msdl-bin)

            **macOS/Linux (no Homebrew/AUR):**
            ```bash
            curl -fsSL https://api.msdl.tech-latest.com/install.sh | bash
            ```
            Auto-detects OS/arch (including Termux on Android) and installs the latest release.

            **Linux/macOS (direct download):**
            ```bash
            # Linux x86_64
            curl -L https://github.com/starkSV/windows-iso-downloader/releases/download/${{ github.ref_name }}/msdl-linux-amd64 -o msdl
            chmod +x msdl && sudo mv msdl /usr/local/bin/

            # Linux ARM64 (also works in Termux on Android)
            curl -L https://github.com/starkSV/windows-iso-downloader/releases/download/${{ github.ref_name }}/msdl-linux-arm64 -o msdl
            chmod +x msdl && mv msdl $PREFIX/bin/ 2>/dev/null || sudo mv msdl /usr/local/bin/

            # macOS Apple Silicon
            curl -L https://github.com/starkSV/windows-iso-downloader/releases/download/${{ github.ref_name }}/msdl-darwin-arm64 -o msdl
            chmod +x msdl && sudo mv msdl /usr/local/bin/

            # macOS Intel
            curl -L https://github.com/starkSV/windows-iso-downloader/releases/download/${{ github.ref_name }}/msdl-darwin-amd64 -o msdl
            chmod +x msdl && sudo mv msdl /usr/local/bin/
            ```

            ## Usage
            ```
            msdl                                              # fully interactive (includes Server/Enterprise)
            msdl "windows 11 25h2"                            # filter products, pick language
            msdl --id 3262 --lang "English"                   # direct, no prompts
            msdl --eval server-2025                          # evaluation ISOs directly
            msdl --list                                       # list all products
            ```

  submit-winget:
    # wingetcreate is a native Windows tool (needs .NET 6 + VC++ Redistributable) --
    # it is NOT published as a NuGet/dotnet-tool package, so it can only run on a
    # windows-latest runner, not ubuntu-latest like the release job above.
    needs: release
    runs-on: windows-latest
    steps:
      - name: Submit winget update
        shell: bash
        env:
          WINGET_TOKEN: ${{ secrets.WINGET_TOKEN }}
        run: |
          if [ -z "$WINGET_TOKEN" ]; then
            echo "WINGET_TOKEN not set, skipping winget update"
            exit 0
          fi
          curl -L -o wingetcreate.exe https://github.com/microsoft/winget-create/releases/latest/download/wingetcreate.exe
          VERSION="${{ github.ref_name }}"
          VERSION="${VERSION#cli/v}"
          ./wingetcreate.exe update starkSV.msdl \
            --version "$VERSION" \
            --urls "https://github.com/starkSV/windows-iso-downloader/releases/download/${{ github.ref_name }}/msdl-windows-amd64.exe" \
            --submit \
            --token "$WINGET_TOKEN"
