name: Frontend Code Check

on:
  pull_request:
    # Only run it when Frontend code changes
    paths:
      - 'src/**'
      - 'tsup.config.ts'
      - 'package.json'

jobs:
  check:
    strategy:
      matrix:
        platform: [ubuntu-latest, windows-latest, macos-latest]
    
    runs-on: ${{ matrix.platform }}

    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      with:
        ref: ${{ github.head_ref }}  
        fetch-depth: 0               

    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20'

    # No need to pass the version arg as it is specified by "packageManager" in package.json
    - name: Install pnpm
      uses: pnpm/action-setup@v4

    - name: Install dependencies
      run: pnpm install --frozen-lockfile

    - name: Switch platformAdapter to Web adapter
      shell: bash
      run: >
        node -e "const fs=require('fs');const f='src/utils/platformAdapter.ts';
        let s=fs.readFileSync(f,'utf8');
        s=s.replace(/import\\s*\\{\\s*createTauriAdapter\\s*\\}\\s*from\\s*\\\"\\.\\/tauriAdapter\\\";/,'import { createWebAdapter } from \\\"./webAdapter\\\";');
        s=s.replace(/let\\s+platformAdapter\\s*=\\s*createTauriAdapter\\(\\);/,'let platformAdapter = createWebAdapter();');
        fs.writeFileSync(f,s);"

    - name: Build web (Tauri dependency check)
      run: pnpm build:web

    - name: Verify no Tauri refs in web output
      shell: bash
      run: |
        if grep -R -n -E '@tauri-apps|tauri-plugin' out/search-chat; then
          echo 'Tauri references found in web build output';
          exit 1;
        else
          echo 'No Tauri references found';
        fi

    - name: Restore platformAdapter to Tauri adapter
      shell: bash
      run: >
        node -e "const fs=require('fs');const f='src/utils/platformAdapter.ts';
        let s=fs.readFileSync(f,'utf8');
        s=s.replace(/import\\s*\\{\\s*createWebAdapter\\s*\\}\\s*from\\s*\\\"\\.\\/webAdapter\\\";/,'import { createTauriAdapter } from \\\"./tauriAdapter\\\";');
        s=s.replace(/let\\s+platformAdapter\\s*=\\s*createWebAdapter\\(\\);/,'let platformAdapter = createTauriAdapter();');
        fs.writeFileSync(f,s);"

    - name: Build frontend
      run: pnpm build
