name: CI

on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  lint-and-test:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Install Linux dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev

      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt

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

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

      - name: Install frontend dependencies
        run: npm ci

      - name: ESLint
        run: npm run lint

      - name: Rust fmt check
        run: cd src-tauri && cargo fmt --check

      - name: Rust clippy
        run: cd src-tauri && cargo clippy -- -D warnings

      - name: Rust tests
        run: cd src-tauri && cargo test

      - name: Frontend tests
        run: npm test

  build:
    needs: lint-and-test
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: macos-latest
            args: --target aarch64-apple-darwin
            rust-target: aarch64-apple-darwin
            artifact-name: tusk-macos-arm64
          - platform: macos-13
            args: --target x86_64-apple-darwin
            rust-target: x86_64-apple-darwin
            artifact-name: tusk-macos-x64
          - platform: windows-latest
            args: ""
            rust-target: ""
            artifact-name: tusk-windows-x64
          - platform: ubuntu-22.04
            args: ""
            rust-target: ""
            artifact-name: tusk-linux-x64

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

    steps:
      - uses: actions/checkout@v4

      - name: Install Linux dependencies
        if: matrix.platform == 'ubuntu-22.04'
        run: |
          sudo apt-get update
          sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev

      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.rust-target }}

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

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

      - name: Install frontend dependencies
        run: npm ci

      - name: Build Tauri app
        uses: tauri-apps/tauri-action@v0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          args: ${{ matrix.args }}

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact-name }}
          path: |
            src-tauri/target/**/release/bundle/deb/*.deb
            src-tauri/target/**/release/bundle/rpm/*.rpm
            src-tauri/target/**/release/bundle/appimage/*.AppImage
            src-tauri/target/**/release/bundle/dmg/*.dmg
            src-tauri/target/**/release/bundle/nsis/*.exe
            src-tauri/target/**/release/bundle/msi/*.msi
          if-no-files-found: ignore
