name: Check Backend

on:
  pull_request:

jobs:
  check-backend:
    runs-on: ubuntu-24.04

    permissions:
      contents: read
      pull-requests: read

    steps:
      - name: Check backend changes
        id: changes
        uses: dorny/paths-filter@v3
        with:
          filters: |
            backend:
              - 'src-tauri/**'
              - 'src-plugin/**'
              - '.github/workflows/check-backend.yml'

      - name: Skip backend checks when unchanged
        if: steps.changes.outputs.backend != 'true'
        run: echo "No backend changes detected, skipping backend checks."

      - name: Checkout code
        if: steps.changes.outputs.backend == 'true'
        uses: actions/checkout@v4

      - name: Setup Rust
        if: steps.changes.outputs.backend == 'true'
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

      - name: Rust cache
        if: steps.changes.outputs.backend == 'true'
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: |
            src-tauri -> target
            src-plugin -> target
            src-plugin/examples -> target

      - name: Install dependencies
        if: steps.changes.outputs.backend == 'true'
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf

      - name: Run Clippy
        if: steps.changes.outputs.backend == 'true'
        working-directory: src-tauri
        run: cargo clippy -- -D warnings

      - name: Run Plugin Clippy
        if: steps.changes.outputs.backend == 'true'
        working-directory: src-plugin
        run: cargo clippy --workspace --all-targets -- -D warnings

      - name: Run Plugin Examples Clippy
        if: steps.changes.outputs.backend == 'true'
        working-directory: src-plugin/examples
        run: cargo clippy --workspace --all-targets -- -D warnings
