on:
  push:
    branches: [master]
    tags:
      - "v*"
  pull_request:
    branches: [master]
name: CI
jobs:
  test:
    name: "Unit Tests"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version: "1.24"
      - uses: extractions/setup-just@v2
      - name: Run tests
        run: just test
      - name: Generate coverage report
        run: just test-coverage
      - name: Display coverage summary
        run: |
          echo "## Test Coverage Summary" >> $GITHUB_STEP_SUMMARY
          echo '```' >> $GITHUB_STEP_SUMMARY
          go tool cover -func=coverage.out >> $GITHUB_STEP_SUMMARY
          echo '```' >> $GITHUB_STEP_SUMMARY
      - name: Upload coverage report
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: coverage.out

  webextension:
    name: "WebExtension"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Bundle extension
        run: |
          mkdir -p /tmp/webextension
          cp -r webextension/* /tmp/webextension/
          cd /tmp/webextension && zip -r "${{ github.workspace }}/switchyard-webextension.zip" .
      - uses: actions/upload-artifact@v4
        with:
          name: switchyard-webextension
          path: switchyard-webextension.zip

  flatpak:
    name: "Flatpak (${{ matrix.variant.arch }})"
    runs-on: ${{ matrix.variant.runner }}
    strategy:
      matrix:
        variant:
          - arch: x86_64
            runner: ubuntu-latest
          - arch: aarch64
            runner: ubuntu-24.04-arm
    container:
      image: ghcr.io/flathub-infra/flatpak-github-actions:gnome-49
      options: --privileged
    steps:
      - uses: actions/checkout@v4
      - uses: flatpak/flatpak-github-actions/flatpak-builder@v6
        with:
          bundle: io.github.alyraffauf.Switchyard.Devel-${{ github.sha }}-${{ matrix.variant.arch }}.flatpak
          manifest-path: flatpak/io.github.alyraffauf.Switchyard.Devel.yml
          cache-key: flatpak-builder
          arch: ${{ matrix.variant.arch }}

  release:
    name: "Create Release"
    if: startsWith(github.ref, 'refs/tags/')
    needs: [flatpak, test, webextension]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download all Flatpak artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: io.github.alyraffauf.Switchyard.Devel-${{ github.sha }}-*.flatpak
          merge-multiple: true
      - name: Download extension artifact
        uses: actions/download-artifact@v4
        with:
          name: switchyard-webextension

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            *.flatpak
            switchyard-webextension.zip
          draft: false
          prerelease: false
          generate_release_notes: true
          fail_on_unmatched_files: true
