name: Build Desktop

on:
  push:
    tags:
      - 'v*'
  release:
    types: [published]
  workflow_dispatch:

permissions:
  contents: write

jobs:
  create-release:
    runs-on: ubuntu-22.04
    outputs:
      tag_name: ${{ steps.release-tag.outputs.tag_name }}
    steps:
      - name: Resolve release tag
        id: release-tag
        shell: bash
        run: |
          if [[ "${{ github.event_name }}" == "release" ]]; then
            echo "tag_name=${{ github.event.release.tag_name }}" >> "$GITHUB_OUTPUT"
          else
            echo "tag_name=${{ github.ref_name }}" >> "$GITHUB_OUTPUT"
          fi

      - name: Checkout
        if: github.event_name == 'push'
        uses: actions/checkout@v4

      - name: Extract changelog section
        if: github.event_name == 'push'
        id: changelog
        shell: bash
        run: |
          # Pull the CHANGELOG.md section whose header matches the tag, e.g. "## [v0.7.0]".
          notes="$(awk -v tag="$TAG_NAME" '
            index($0, "## [" tag "]") == 1 { capture = 1; next }
            capture && index($0, "## [") == 1 { exit }
            capture { print }
          ' CHANGELOG.md)"
          if [[ -z "${notes// }" ]]; then
            echo "No CHANGELOG.md entry found for $TAG_NAME; falling back to generated notes only."
          fi
          {
            echo "notes<<__CHANGELOG_EOF__"
            echo "$notes"
            echo "__CHANGELOG_EOF__"
          } >> "$GITHUB_OUTPUT"
        env:
          TAG_NAME: ${{ steps.release-tag.outputs.tag_name }}

      - name: Create Release for tag
        if: github.event_name == 'push'
        shell: bash
        run: |
          printf '%s\n' "${NOTES}" > release-notes.md
          gh release create "$TAG_NAME" \
            --title "$TAG_NAME" \
            --notes-file release-notes.md \
            --generate-notes || gh release view "$TAG_NAME"
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GH_REPO: ${{ github.repository }}
          TAG_NAME: ${{ steps.release-tag.outputs.tag_name }}
          NOTES: ${{ steps.changelog.outputs.notes }}

  build:
    needs: create-release
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-latest
            asset: suzent-windows-x86_64.exe
            binary: src-tauri/target/release/suzent.exe
            installer_asset: suzent-installer-windows-x86_64.exe
            installer_binary: apps/suzent-installer/target/release/suzent-installer.exe
          - os: macos-latest
            asset: suzent-macos-aarch64
            binary: src-tauri/target/release/suzent
            installer_asset: suzent-installer-macos-aarch64
            installer_binary: apps/suzent-installer/target/release/suzent-installer
          - os: macos-13
            asset: suzent-macos-x86_64
            binary: src-tauri/target/release/suzent
            installer_asset: suzent-installer-macos-x86_64
            installer_binary: apps/suzent-installer/target/release/suzent-installer
          - os: ubuntu-22.04
            asset: suzent-linux-x86_64
            binary: src-tauri/target/release/suzent
            installer_asset: suzent-installer-linux-x86_64
            installer_binary: apps/suzent-installer/target/release/suzent-installer

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4

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

      - uses: dtolnay/rust-toolchain@stable

      - uses: Swatinem/rust-cache@v2
        with:
          workdir: src-tauri

      - name: Install Linux system deps
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            libwebkit2gtk-4.1-dev \
            libayatana-appindicator3-dev \
            librsvg2-dev \
            patchelf

      - name: Install frontend deps
        run: cd frontend && npm ci

      - name: Install Tauri JS deps
        run: cd src-tauri && npm ci

      - name: Install installer JS deps
        run: cd apps/suzent-installer && npm ci

      - name: Build main app
        run: cd src-tauri && npm run build:dist

      - name: Build installer app
        run: cd apps/suzent-installer && npm run build

      - name: Upload binaries to Release
        if: github.event_name == 'release' || github.event_name == 'push'
        shell: bash
        run: |
          cp "${{ matrix.binary }}" "${{ matrix.asset }}"
          cp "${{ matrix.installer_binary }}" "${{ matrix.installer_asset }}"
          gh release upload "${{ needs.create-release.outputs.tag_name }}" \
            "${{ matrix.asset }}" \
            "${{ matrix.installer_asset }}" \
            --clobber
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
