name: Stable Release

on:
  push:
    branches: [main]

jobs:
  stable:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.24'

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Cache Go modules
        uses: actions/cache@v4
        with:
          path: |
            /root/go/pkg/mod
            /home/runner/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-

      - name: Cache Node modules
        uses: actions/cache@v4
        with:
          path: web/node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('web/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Download dependencies
        run: go mod download

      - name: Build frontend
        run: |
          cd web
          npm ci
          npm run build

      - name: Vet
        run: go vet ./...

      - name: Test
        run: go test ./... -v -race -timeout 60s

      - name: Determine version
        id: version
        run: |
          BASE_VERSION=$(grep 'Version =' internal/version/version.go | cut -d'"' -f2)
          VERSION="v${BASE_VERSION}"
          echo "version=${VERSION}" >> $GITHUB_OUTPUT
          echo "base=${BASE_VERSION}" >> $GITHUB_OUTPUT
          echo "Building stable release: ${VERSION}"

      - name: Generate Windows resource (icon)
        run: |
          go install github.com/akavel/rsrc@latest
          RSRC="$(go env GOPATH)/bin/rsrc"
          $RSRC -ico assets/muyue.ico -arch amd64 -o cmd/muyue/rsrc_windows_amd64.syso
          $RSRC -ico assets/muyue.ico -arch arm64 -o cmd/muyue/rsrc_windows_arm64.syso

      - name: Build (all platforms)
        run: |
          mkdir -p dist
          LDFLAGS="-s -w"
          # Windows builds use -H=windowsgui so the binary registers as a GUI
          # subsystem app: double-clicking from the Desktop shortcut does not
          # spawn a console window (and huh's "This is a command line tool"
          # banner can never appear).
          WIN_LDFLAGS="$LDFLAGS -H=windowsgui"
          CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="$LDFLAGS" -o dist/muyue-linux-amd64 ./cmd/muyue/
          CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="$LDFLAGS" -o dist/muyue-linux-arm64 ./cmd/muyue/
          CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="$LDFLAGS" -o dist/muyue-darwin-amd64 ./cmd/muyue/
          CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="$LDFLAGS" -o dist/muyue-darwin-arm64 ./cmd/muyue/
          CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="$WIN_LDFLAGS" -o dist/muyue-windows-amd64.exe ./cmd/muyue/
          CGO_ENABLED=0 GOOS=windows GOARCH=arm64 go build -ldflags="$WIN_LDFLAGS" -o dist/muyue-windows-arm64.exe ./cmd/muyue/

      - name: Package archives
        run: |
          cd dist
          sha256sum * > checksums.txt
          tar czf muyue-linux-amd64.tar.gz muyue-linux-amd64
          tar czf muyue-linux-arm64.tar.gz muyue-linux-arm64
          tar czf muyue-darwin-amd64.tar.gz muyue-darwin-amd64
          tar czf muyue-darwin-arm64.tar.gz muyue-darwin-arm64
          zip muyue-windows-amd64.zip muyue-windows-amd64.exe
          zip muyue-windows-arm64.zip muyue-windows-arm64.exe
          rm -f muyue-linux-amd64 muyue-linux-arm64 muyue-darwin-amd64 muyue-darwin-arm64 muyue-windows-amd64.exe muyue-windows-arm64.exe

      - name: Generate changelog
        id: changelog
        run: |
          PREV_TAG=$(git tag -l 'v[0-9]*.[0-9]*.[0-9]*' --sort=-v:refname | grep -v '-' | head -1 || echo "")
          if [ -n "$PREV_TAG" ]; then
            RANGE="${PREV_TAG}..HEAD"
            echo "range=${RANGE}" >> $GITHUB_OUTPUT
          else
            RANGE="HEAD~30..HEAD"
            echo "range=${RANGE}" >> $GITHUB_OUTPUT
          fi
          VERSION=${{ steps.version.outputs.version }}
          DL_URL="${{ github.server_url }}/${{ github.repository }}/releases/download/${VERSION}"
          {
            echo "## ${VERSION}"
            echo ""
            echo "### Changes since ${PREV_TAG:-start}"
            echo ""
            git log ${RANGE} --pretty=format:"- %s (%h)" --no-merges
            echo ""
            echo ""
            echo "### Downloads"
            echo ""
            echo "| Platform | File |"
            echo "|----------|------|"
            echo "| Linux x86_64 | [muyue-linux-amd64.tar.gz](${DL_URL}/muyue-linux-amd64.tar.gz) |"
            echo "| Linux ARM64 | [muyue-linux-arm64.tar.gz](${DL_URL}/muyue-linux-arm64.tar.gz) |"
            echo "| macOS Intel | [muyue-darwin-amd64.tar.gz](${DL_URL}/muyue-darwin-amd64.tar.gz) |"
            echo "| macOS Apple Silicon | [muyue-darwin-arm64.tar.gz](${DL_URL}/muyue-darwin-arm64.tar.gz) |"
            echo "| Windows x86_64 | [muyue-windows-amd64.zip](${DL_URL}/muyue-windows-amd64.zip) |"
            echo "| Windows ARM64 | [muyue-windows-arm64.zip](${DL_URL}/muyue-windows-arm64.zip) |"
            echo ""
            echo "The binary includes both CLI and Desktop modes."
            echo "Run \`muyue\` for TUI, \`muyue desktop\` for web UI."
            echo ""
            echo "### Install"
            echo ""
            echo "**Linux (x86_64)**"
            echo "\`\`\`bash"
            echo "curl -sL ${DL_URL}/muyue-linux-amd64.tar.gz | tar xz"
            echo "chmod +x muyue-linux-amd64"
            echo "sudo mv muyue-linux-amd64 /usr/local/bin/muyue"
            echo "\`\`\`"
            echo ""
            echo "**macOS (Apple Silicon)**"
            echo "\`\`\`bash"
            echo "curl -sL ${DL_URL}/muyue-darwin-arm64.tar.gz | tar xz"
            echo "chmod +x muyue-darwin-arm64"
            echo "sudo mv muyue-darwin-arm64 /usr/local/bin/muyue"
            echo "\`\`\`"
            echo ""
            echo "**Windows (x86_64)** — sans privilèges admin, crée les raccourcis Bureau + Menu Démarrer + commande \`muyue\` dans la session courante :"
            echo "\`\`\`powershell"
            echo "Get-Process muyue, muyue-windows-amd64 -ErrorAction SilentlyContinue | Stop-Process -Force; Start-Sleep -Milliseconds 500"
            echo "\$dest = \"\$env:LOCALAPPDATA\\Muyue\"; New-Item -ItemType Directory -Force -Path \$dest | Out-Null"
            echo "Invoke-WebRequest -Uri \"${DL_URL}/muyue-windows-amd64.zip\" -OutFile \"\$env:TEMP\\muyue.zip\""
            echo "Expand-Archive -Path \"\$env:TEMP\\muyue.zip\" -DestinationPath \$dest -Force"
            echo "& \"\$dest\\muyue-windows-amd64.exe\" install-shortcuts"
            echo "\$env:Path += \";\$dest\""
            echo "\`\`\`"
            echo ""
            echo "Le 1ʳᵉ ligne tue toute instance Muyue déjà lancée (sinon Windows refuse d'écraser le \`.exe\` verrouillé et l'install échoue silencieusement). Si vous mettez à jour depuis une version précédente, c'est obligatoire."
          } > /tmp/stable_changelog.md
          echo "path=/tmp/stable_changelog.md" >> $GITHUB_OUTPUT

      - name: Update CHANGELOG.md
        run: |
          if [ ! -f CHANGELOG.md ]; then
            echo "# Changelog" > CHANGELOG.md
            echo "" >> CHANGELOG.md
            echo "All notable changes to this project will be documented in this file." >> CHANGELOG.md
            echo "" >> CHANGELOG.md
            echo "The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)." >> CHANGELOG.md
            echo "" >> CHANGELOG.md
          fi
          NEW_ENTRY=$(cat /tmp/stable_changelog.md)
          EXISTING=$(tail -n +6 CHANGELOG.md)
          {
            head -5 CHANGELOG.md
            echo ""
            echo "$NEW_ENTRY"
            echo ""
            echo "$EXISTING"
          } > CHANGELOG.md.new
          mv CHANGELOG.md.new CHANGELOG.md

      - name: Commit changelog
        env:
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
        run: |
          git config user.name "CI Bot"
          git config user.email "ci@legion-muyue.fr"
          git add CHANGELOG.md
          git diff --cached --quiet && echo "No changelog changes" && exit 0
          git commit -m "chore: update CHANGELOG for ${{ steps.version.outputs.version }}"
          git push

      - name: Create release
        env:
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
        run: |
          set -ex
          if [ -z "$GITEA_TOKEN" ]; then
            echo "Error: GITEA_TOKEN secret is not set"
            exit 1
          fi
          VERSION=${{ steps.version.outputs.version }}
          API="${{ github.server_url }}/api/v1/repos/${{ github.repository }}/releases"
          echo "Creating release ${VERSION} at ${API}"

          EXISTING=$(curl -sf -H "Authorization: token ${GITEA_TOKEN}" "${API}/tags/${VERSION}" || echo "")
          if [ -n "$EXISTING" ]; then
            EXISTING_ID=$(echo "$EXISTING" | python3 -c "import sys,json; print(json.load(sys.stdin).get('id',''))" 2>/dev/null || echo "")
            if [ -n "$EXISTING_ID" ]; then
              echo "Release ${VERSION} already exists (ID: ${EXISTING_ID}), deleting..."
              curl -sf -X DELETE -H "Authorization: token ${GITEA_TOKEN}" "${API}/${EXISTING_ID}" || true
            fi
          fi

          BODY=$(python3 -c "import json,sys; print(json.dumps(sys.stdin.read()))" < /tmp/stable_changelog.md)
          RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "${API}" \
            -H "Authorization: token ${GITEA_TOKEN}" \
            -H "Content-Type: application/json" \
            -d "{
              \"tag_name\":\"${VERSION}\",
              \"target_commitish\":\"main\",
              \"name\":\"muyue ${VERSION}\",
              \"body\":${BODY},
              \"draft\":false,
              \"prerelease\":false
            }")
          HTTP_CODE=$(echo "$RESPONSE" | tail -1)
          RESPONSE_BODY=$(echo "$RESPONSE" | sed '$d')
          echo "HTTP Status: ${HTTP_CODE}"
          echo "Response: ${RESPONSE_BODY}"
          RELEASE_ID=$(echo "$RESPONSE_BODY" | python3 -c "import sys,json; print(json.load(sys.stdin).get('id',''))" 2>/dev/null || echo "")
          if [ -z "$RELEASE_ID" ]; then
            echo "Failed to create release"
            exit 1
          fi
          echo "Release ID: ${RELEASE_ID}"
          UPLOAD_URL="${{ github.server_url }}/api/v1/repos/${{ github.repository }}/releases/${RELEASE_ID}/assets"
          for file in dist/*.tar.gz dist/*.zip dist/checksums.txt; do
            filename=$(basename "$file")
            echo "Uploading ${filename}..."
            UPLOAD_RESP=$(curl -s -w "\n%{http_code}" -X POST "${UPLOAD_URL}" \
              -H "Authorization: token ${GITEA_TOKEN}" \
              -F "attachment=@${file};filename=${filename}")
            UPLOAD_CODE=$(echo "$UPLOAD_RESP" | tail -1)
            if [ "$UPLOAD_CODE" != "201" ]; then
              echo "Upload failed with status ${UPLOAD_CODE}"
            fi
          done
          echo "Stable release ${VERSION} published!"
