name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  workflow_dispatch:

jobs:
  lint-and-build:
    runs-on: ubuntu-latest
    container:
      image: ubuntu:22.04
    env:
      DEBIAN_FRONTEND: noninteractive
      APPIMAGE_EXTRACT_AND_RUN: "1"
    steps:
      - name: Install system dependencies
        run: |
          apt-get update
          apt-get install -y \
            build-essential curl wget pkg-config git ca-certificates \
            libgtk-3-dev libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev \
            libssl-dev xdg-utils

      - name: Checkout
        run: |
          git clone --depth=1 --branch="${GITHUB_REF_NAME}" "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" .

      - name: Install Node.js 22
        run: |
          curl -fsSL https://deb.nodesource.com/setup_22.x | bash -
          apt-get install -y nodejs

      - name: Install Rust toolchain
        run: |
          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable --component clippy,rustfmt
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Install frontend dependencies
        run: npm ci

      - name: ESLint
        run: npm run lint

      - name: Rust fmt check
        run: |
          . "$HOME/.cargo/env"
          cd src-tauri && cargo fmt --check

      - name: Rust clippy
        run: |
          . "$HOME/.cargo/env"
          cd src-tauri && cargo clippy -- -D warnings

      - name: Rust tests
        run: |
          . "$HOME/.cargo/env"
          cd src-tauri && cargo test

      - name: Build Tauri app
        run: |
          . "$HOME/.cargo/env"
          npm run tauri build -- --bundles deb,rpm
