name: CI
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

env:
  CARGO_TERM_COLOR: always

jobs:
  check:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v4

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y clang pkg-config libxcb1-dev libxcb-render0-dev \
            libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev libssl-dev \
            libgtk-3-dev libfontconfig1-dev libdbus-1-dev

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

      - uses: Swatinem/rust-cache@v2

      - name: Remove local-only MCP dependency
        run: |
          # Remove the path dependency line but keep the feature (as empty)
          # First, change the mcp feature to empty (must do before removing dep line)
          sed -i 's/mcp = \["dep:egui-mcp-bridge"\]/mcp = []/' Cargo.toml
          # Then remove the optional dependency line
          sed -i '/^egui-mcp-bridge = /d' Cargo.toml

      - run: cargo fmt --check
      - run: cargo clippy -- -D warnings
      - run: cargo build --release
      - run: cargo test

  version-sync:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v4
      - name: Check versions match
        run: |
          # Robust extraction tolerating quotes and spacing
          CARGO_VER=$(awk -F'"' '/^version[[:space:]]*=[[:space:]]*"/{print $2; exit}' Cargo.toml)
          SNAP_VER=$(sed -n "s/^version:[[:space:]]*['\"]\\?\\([0-9]\\+\\.[0-9]\\+\\.[0-9]\\+\\)['\"]\\?/\\1/p" snap/snapcraft.yaml)

          echo "Cargo.toml: $CARGO_VER"
          echo "snapcraft.yaml: $SNAP_VER"

          if [ "$CARGO_VER" != "$SNAP_VER" ]; then
            echo "::error::Version mismatch! Cargo=$CARGO_VER Snap=$SNAP_VER"
            exit 1
          fi
