# Installs Node.js dependencies and bun, and checks formatting + linting

name: Lint Node.js

on:
  push:
    branches:
      - main
  pull_request:
    paths-ignore:
      - "src-tauri/**"
      - "README.md"

jobs:
  build:
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-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: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20.10.0'
          cache: "npm"

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: src-tauri

      - name: Install Linux dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.1-dev libglib2.0-dev
          # Install FFmpeg dependencies
          sudo apt-get install -y ffmpeg libavcodec-dev libavformat-dev libavutil-dev libavfilter-dev libavdevice-dev libswscale-dev libswresample-dev pkg-config
          # Set PKG_CONFIG_PATH
          echo "PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig" >> $GITHUB_ENV

      - name: Install Windows dependencies for Tauri bindings
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          # Install pkg-config
          choco install pkgconfiglite -y
          
          # Create FFmpeg directory
          New-Item -ItemType Directory -Force -Path C:\ffmpeg
          
          # Download pre-built FFmpeg shared libraries
          $ffmpegUrl = "https://github.com/GyanD/codexffmpeg/releases/download/7.1/ffmpeg-7.1-full_build-shared.7z"
          Write-Host "Downloading FFmpeg from $ffmpegUrl"
          Invoke-WebRequest -Uri $ffmpegUrl -OutFile ffmpeg.7z
          
          # Extract FFmpeg
          Write-Host "Extracting FFmpeg..."
          7z x ffmpeg.7z -oC:\
          $extractedDir = Get-ChildItem -Path C:\ -Directory | Where-Object { $_.Name -like "ffmpeg-*" } | Select-Object -First 1
          Write-Host "Found extracted directory: $($extractedDir.FullName)"
          
          # Copy contents to C:\ffmpeg
          Write-Host "Copying FFmpeg files to C:\ffmpeg..."
          Copy-Item -Path "$($extractedDir.FullName)\*" -Destination C:\ffmpeg -Recurse -Force
          
          # Set environment variables using NO_PKG_CONFIG approach
          Write-Host "Setting environment variables..."
          echo "FFMPEG_DIR=C:\ffmpeg" >> $env:GITHUB_ENV
          echo "FFMPEG_INCLUDE_DIR=C:\ffmpeg\include" >> $env:GITHUB_ENV
          echo "FFMPEG_LIB_DIR=C:\ffmpeg\lib" >> $env:GITHUB_ENV
          echo "FFMPEG_NO_PKG_CONFIG=1" >> $env:GITHUB_ENV
          echo "RUSTFLAGS=-C link-arg=/LIBPATH:C:\ffmpeg\lib" >> $env:GITHUB_ENV
          echo "C:\ffmpeg\bin" >> $env:GITHUB_PATH

      - name: Create dist directory
        run: mkdir -p dist

      - name: Build Rust dependencies on Unix
        if: runner.os != 'Windows'
        env:
          PKG_CONFIG_PATH: /usr/lib/x86_64-linux-gnu/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig
        run: |
          cd src-tauri
          cargo build --release

      - name: Generate TypeScript bindings on Unix
        if: runner.os != 'Windows'
        env:
          PKG_CONFIG_PATH: /usr/lib/x86_64-linux-gnu/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig
        run: |
          cd src-tauri
          cargo run --bin generate_types

      - name: Build Rust dependencies on Windows
        if: runner.os == 'Windows'
        shell: pwsh
        env:
          FFMPEG_DIR: C:\ffmpeg
          FFMPEG_INCLUDE_DIR: C:\ffmpeg\include
          FFMPEG_LIB_DIR: C:\ffmpeg\lib
          FFMPEG_NO_PKG_CONFIG: 1
          RUSTFLAGS: -C link-arg=/LIBPATH:C:\ffmpeg\lib
        run: |
          cd src-tauri
          cargo build --release

      - name: Generate TypeScript bindings on Windows
        if: runner.os == 'Windows'
        shell: pwsh
        env:
          FFMPEG_DIR: C:\ffmpeg
          FFMPEG_INCLUDE_DIR: C:\ffmpeg\include
          FFMPEG_LIB_DIR: C:\ffmpeg\lib
          FFMPEG_NO_PKG_CONFIG: 1
          RUSTFLAGS: -C link-arg=/LIBPATH:C:\ffmpeg\lib
        run: |
          cd src-tauri
          cargo run --bin generate_types

      - name: Install dependencies on Unix
        if: runner.os != 'Windows'
        run: |
          npm config set fetch-timeout 300000
          npm config set fetch-retries 5
          npm ci --prefer-offline --no-audit --no-fund

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

      - name: Run lint step
        env:
          NODE_OPTIONS: '--max-old-space-size=4096'
        run: npm run lint:ci
