# Installs Rust and checks formatting + linting

name: Lint Rust

on:
  push:
    branches:
      - main
  pull_request:
    paths-ignore:
      - "src/**"
      - "package.json"
      - "package-lock.json"
      - "bun.lock"
      - "pnpm-lock.yaml"
      - "README.md"

jobs:
  build:
    strategy:
      fail-fast: true
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]

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

    steps:
      - name: Disable git core.autocrlf on Windows
        if: matrix.os == 'windows-latest'
        run: git config --global core.autocrlf false

      - name: Checkout repository code
        uses: actions/checkout@v4

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

      - name: Install Linux dependencies
        if: matrix.os == 'ubuntu-latest'
        run: |
          sudo apt-get update
          # Install essential build tools and C/C++ headers first
          sudo apt-get install -y build-essential gcc g++ make cmake
          # Install Clang and LLVM for bindgen
          sudo apt-get install -y clang libclang-dev llvm-dev
          # Try to install specific clang version if available
          sudo apt-get install -y libclang-14-dev || sudo apt-get install -y libclang-13-dev || sudo apt-get install -y libclang-12-dev || true
          # Install standard library headers
          sudo apt-get install -y libc6-dev linux-libc-dev
          # Install other required dependencies
          sudo apt-get install -y libwebkit2gtk-4.1-dev curl wget file libxdo-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev
          # Install FFmpeg and development libraries
          sudo apt-get install -y ffmpeg libavcodec-dev libavformat-dev libavutil-dev libavfilter-dev libavdevice-dev libswscale-dev libswresample-dev pkg-config
          
      - name: Setup FFmpeg environment for Ubuntu
        if: matrix.os == 'ubuntu-latest'
        run: |
          echo "=== FFmpeg Header Location Fix ==="
          
          # Check if headers are in expected location
          if [ ! -f "/usr/include/libswscale/swscale.h" ]; then
            echo "Headers not found in /usr/include, searching..."
            
            # Find actual location
            SWSCALE_PATHS=$(find /usr -name "swscale.h" 2>/dev/null)
            echo "Found swscale.h at: $SWSCALE_PATHS"
            
            # Find main header location
            MAIN_SWSCALE=$(echo "$SWSCALE_PATHS" | grep "libswscale/swscale.h" | head -1)
            
            if [ -n "$MAIN_SWSCALE" ]; then
              BASE_DIR=$(dirname $(dirname "$MAIN_SWSCALE"))
              echo "Found FFmpeg headers in: $BASE_DIR"
              
              # Create symlinks
              FFMPEG_LIBS=("libavcodec" "libavformat" "libavutil" "libswscale" "libavfilter" "libavdevice" "libswresample")
              for lib in "${FFMPEG_LIBS[@]}"; do
                if [ -d "$BASE_DIR/$lib" ] && [ ! -L "/usr/include/$lib" ]; then
                  sudo ln -sfn "$BASE_DIR/$lib" "/usr/include/$lib"
                  echo "✅ Created symlink: /usr/include/$lib -> $BASE_DIR/$lib"
                fi
              done
            fi
          fi
          
          # Set PKG_CONFIG_PATH
          echo "PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig" >> $GITHUB_ENV
          
          # Find and set up libclang for bindgen
          echo "=== Finding libclang ==="
          # Find libclang.so
          LIBCLANG_PATH=$(find /usr -name "libclang.so*" -type f 2>/dev/null | grep -E "libclang.so|libclang-[0-9]+.so" | head -1)
          if [ -n "$LIBCLANG_PATH" ]; then
            LIBCLANG_DIR=$(dirname "$LIBCLANG_PATH")
            echo "Found libclang at: $LIBCLANG_PATH"
            echo "Setting LIBCLANG_PATH=$LIBCLANG_DIR"
            echo "LIBCLANG_PATH=$LIBCLANG_DIR" >> $GITHUB_ENV
          else
            echo "WARNING: libclang.so not found, trying default paths"
            # Try common paths
            for path in /usr/lib/llvm-*/lib /usr/lib/x86_64-linux-gnu /usr/lib64 /usr/lib; do
              if [ -d "$path" ] && ls "$path"/libclang*.so* 2>/dev/null | grep -q .; then
                echo "Found libclang in: $path"
                echo "LIBCLANG_PATH=$path" >> $GITHUB_ENV
                break
              fi
            done
          fi
          
          # Set up bindgen clang args
          echo "BINDGEN_EXTRA_CLANG_ARGS=-I/usr/include -I/usr/include/x86_64-linux-gnu" >> $GITHUB_ENV

      - name: Install macOS dependencies
        if: matrix.os == 'macos-latest'
        run: |
          brew install ffmpeg pkg-config
          echo "PKG_CONFIG_PATH=/opt/homebrew/lib/pkgconfig:/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH" >> $GITHUB_ENV
          echo "FFMPEG_DIR=/opt/homebrew/opt/ffmpeg" >> $GITHUB_ENV
          echo "FFMPEG_INCLUDE_DIR=/opt/homebrew/opt/ffmpeg/include" >> $GITHUB_ENV
          echo "FFMPEG_LIB_DIR=/opt/homebrew/opt/ffmpeg/lib" >> $GITHUB_ENV
          
          # Debug FFmpeg installation
          echo "=== FFmpeg Installation Debug ==="
          which ffmpeg || echo "ffmpeg not in PATH"
          pkg-config --exists libavcodec && echo "✅ libavcodec found" || echo "❌ libavcodec not found"
          pkg-config --cflags libavcodec || echo "No cflags"
          pkg-config --libs libavcodec || echo "No libs"
          ls -la /opt/homebrew/opt/ffmpeg/include/ | head -10
          ls -la /opt/homebrew/opt/ffmpeg/lib/ | head -10

      - name: Install Windows dependencies
        if: matrix.os == 'windows-latest'
        run: |
          # Install pkg-config
          choco install pkgconfiglite -y
          
          # Install vcpkg
          git clone https://github.com/Microsoft/vcpkg.git C:\vcpkg
          C:\vcpkg\bootstrap-vcpkg.bat
          C:\vcpkg\vcpkg integrate install
          
          # Install FFmpeg via vcpkg
          C:\vcpkg\vcpkg install ffmpeg:x64-windows
          
          # Set environment variables
          echo "VCPKG_ROOT=C:\vcpkg" >> $GITHUB_ENV
          echo "PKG_CONFIG_PATH=C:\vcpkg\installed\x64-windows\lib\pkgconfig" >> $GITHUB_ENV
          echo "FFMPEG_DIR=C:\vcpkg\installed\x64-windows" >> $GITHUB_ENV
          echo "FFMPEG_INCLUDE_DIR=C:\vcpkg\installed\x64-windows\include" >> $GITHUB_ENV
          echo "FFMPEG_LIB_DIR=C:\vcpkg\installed\x64-windows\lib" >> $GITHUB_ENV
          echo "C:\vcpkg\installed\x64-windows\bin" >> $GITHUB_PATH

      - name: Create empty 'dist' directory
        run: mkdir dist

      - name: Set up Node.js v22
        uses: actions/setup-node@v4
        with:
          node-version-file: .node-version
          cache: "npm"

      - name: Install dependencies on Unix
        if: runner.os != 'Windows'
        run: |
          npm config set fetch-timeout 300000
          npm config set fetch-retries 5
          # Try to install with retries, excluding onnxruntime-node
          npm ci --prefer-offline --no-audit --no-fund --omit=optional || \
          npm install --no-audit --no-fund --omit=optional
          
          # Install onnxruntime-node separately with retry logic
          node ./scripts/install-onnxruntime.js || echo "onnxruntime-node installation failed, continuing..."

      - name: Install dependencies on Windows
        if: runner.os == 'Windows'
        shell: pwsh
        run: ./scripts/windows-npm-install.ps1

      - name: Debug environment on macOS
        if: matrix.os == 'macos-latest'
        run: |
          echo "=== Environment Variables ==="
          echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH"
          echo "FFMPEG_DIR=$FFMPEG_DIR"
          echo "FFMPEG_INCLUDE_DIR=$FFMPEG_INCLUDE_DIR"
          echo "FFMPEG_LIB_DIR=$FFMPEG_LIB_DIR"
          echo ""
          echo "=== Cargo environment ==="
          cd src-tauri
          cargo --version
          rustc --version

      - name: Run rustfmt check
        run: npm run format:rust:check
        env:
          PKG_CONFIG_PATH: ${{ matrix.os == 'macos-latest' && '/opt/homebrew/lib/pkgconfig:/usr/local/lib/pkgconfig' || (matrix.os == 'windows-latest' && 'C:\vcpkg\installed\x64-windows\lib\pkgconfig' || '') }}
          VCPKG_ROOT: ${{ matrix.os == 'windows-latest' && 'C:\vcpkg' || '' }}
          FFMPEG_DIR: ${{ matrix.os == 'macos-latest' && '/opt/homebrew/opt/ffmpeg' || (matrix.os == 'windows-latest' && 'C:\vcpkg\installed\x64-windows' || '') }}
          FFMPEG_INCLUDE_DIR: ${{ matrix.os == 'macos-latest' && '/opt/homebrew/opt/ffmpeg/include' || (matrix.os == 'windows-latest' && 'C:\vcpkg\installed\x64-windows\include' || '') }}
          FFMPEG_LIB_DIR: ${{ matrix.os == 'macos-latest' && '/opt/homebrew/opt/ffmpeg/lib' || (matrix.os == 'windows-latest' && 'C:\vcpkg\installed\x64-windows\lib' || '') }}

      - name: Run clippy check and deny warnings
        run: npm run lint:rust
        env:
          PKG_CONFIG_PATH: ${{ matrix.os == 'macos-latest' && '/opt/homebrew/lib/pkgconfig:/usr/local/lib/pkgconfig' || (matrix.os == 'windows-latest' && 'C:\vcpkg\installed\x64-windows\lib\pkgconfig' || '') }}
          VCPKG_ROOT: ${{ matrix.os == 'windows-latest' && 'C:\vcpkg' || '' }}
          FFMPEG_DIR: ${{ matrix.os == 'macos-latest' && '/opt/homebrew/opt/ffmpeg' || (matrix.os == 'windows-latest' && 'C:\vcpkg\installed\x64-windows' || '') }}
          FFMPEG_INCLUDE_DIR: ${{ matrix.os == 'macos-latest' && '/opt/homebrew/opt/ffmpeg/include' || (matrix.os == 'windows-latest' && 'C:\vcpkg\installed\x64-windows\include' || '') }}
          FFMPEG_LIB_DIR: ${{ matrix.os == 'macos-latest' && '/opt/homebrew/opt/ffmpeg/lib' || (matrix.os == 'windows-latest' && 'C:\vcpkg\installed\x64-windows\lib' || '') }}
