name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build-linux:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: true

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 9

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: pnpm

      - name: Install dependencies
        run: pnpm install

      - name: Build texthooker-ui
        run: |
          cd vendor/texthooker-ui
          pnpm install
          pnpm build

      - name: Build AppImage
        run: pnpm run build:appimage
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload AppImage artifact
        uses: actions/upload-artifact@v4
        with:
          name: appimage
          path: release/*.AppImage

  build-macos:
    runs-on: macos-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: true

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 9

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: pnpm

      - name: Validate macOS signing/notarization secrets
        run: |
          missing=0
          for name in CSC_LINK CSC_KEY_PASSWORD APPLE_ID APPLE_APP_SPECIFIC_PASSWORD APPLE_TEAM_ID; do
            if [ -z "${!name}" ]; then
              echo "Missing required secret: $name"
              missing=1
            fi
          done
          if [ "$missing" -ne 0 ]; then
            echo "Set all required macOS signing/notarization secrets and rerun."
            exit 1
          fi
        env:
          CSC_LINK: ${{ secrets.CSC_LINK }}
          CSC_KEY_PASSWORD: ${{ secrets.CSC_KEY_PASSWORD }}
          APPLE_ID: ${{ secrets.APPLE_ID }}
          APPLE_APP_SPECIFIC_PASSWORD: ${{ secrets.APPLE_APP_SPECIFIC_PASSWORD }}
          APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}

      - name: Install dependencies
        run: pnpm install

      - name: Build texthooker-ui
        run: |
          cd vendor/texthooker-ui
          pnpm install
          pnpm build

      - name: Build signed + notarized macOS artifacts
        run: pnpm run build:mac
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CSC_LINK: ${{ secrets.CSC_LINK }}
          CSC_KEY_PASSWORD: ${{ secrets.CSC_KEY_PASSWORD }}
          APPLE_ID: ${{ secrets.APPLE_ID }}
          APPLE_APP_SPECIFIC_PASSWORD: ${{ secrets.APPLE_APP_SPECIFIC_PASSWORD }}
          APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}

      - name: Upload macOS artifacts
        uses: actions/upload-artifact@v4
        with:
          name: macos
          path: |
            release/*.dmg
            release/*.zip

  release:
    needs: [build-linux, build-macos]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Download AppImage
        uses: actions/download-artifact@v4
        with:
          name: appimage
          path: release

      - name: Download macOS artifacts
        uses: actions/download-artifact@v4
        with:
          name: macos
          path: release

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Verify Bun subminer wrapper
        run: |
          chmod +x subminer
          ./subminer --help >/dev/null

      - name: Get version from tag
        id: version
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Generate changelog
        id: changelog
        run: |
          PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
          if [ -n "$PREV_TAG" ]; then
            CHANGES=$(git log --pretty=format:"- %s" ${PREV_TAG}..HEAD)
          else
            COMMIT_COUNT=$(git rev-list --count HEAD)
            if [ "$COMMIT_COUNT" -gt 10 ]; then
              CHANGES=$(git log --pretty=format:"- %s" HEAD~10..HEAD)
            else
              CHANGES=$(git log --pretty=format:"- %s")
            fi
          fi
          echo "CHANGES<<EOF" >> $GITHUB_OUTPUT
          echo "$CHANGES" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          name: ${{ steps.version.outputs.VERSION }}
          body: |
            ## Changes
            ${{ steps.changelog.outputs.CHANGES }}

            ## Installation

            ### AppImage (Recommended)
            1. Download the AppImage below
            2. Make it executable: `chmod +x SubMiner-*.AppImage`
            3. Run: `./SubMiner-*.AppImage`

            ### macOS
            1. Download `subminer-*.dmg`
            2. Open the DMG and drag `SubMiner.app` into `/Applications`
            3. If needed, use the ZIP artifact as an alternative

            ### Manual Installation
            See the [README](https://github.com/${{ github.repository }}#installation) for manual installation instructions.

            Note: the `subminer` wrapper script uses Bun (`#!/usr/bin/env bun`), so `bun` must be installed and on `PATH`.
          files: |
            release/*.AppImage
            release/*.dmg
            release/*.zip
            subminer
          draft: false
          prerelease: false
