on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
concurrency:
  group: 'pr-${{ github.event.pull_request.number }}'
  cancel-in-progress: true
permissions:
  contents: read
jobs:
  test-vscode:
    env:
      PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-node@v6
        with:
          node-version: '22'
      - uses: pnpm/action-setup@v4
        with:
          version: latest
      - name: Install dependencies
        run: pnpm install
      - name: Run CI
        run: pnpm run ci
  test-vscode-e2e:
    runs-on:
      labels: [ubuntu-2204-8]
    # As at 2026-01-12 this job flakes 100% of the time. It needs investigation
    if: false
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-node@v6
        with:
          node-version: '22'
      - uses: pnpm/action-setup@v4
        with:
          version: latest
      - name: Install dependencies
        run: pnpm install
      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.12'
      - name: Install uv
        uses: astral-sh/setup-uv@v7
      - name: Install python dependencies
        run: |
          python -m venv .venv
          source .venv/bin/activate
          make install-dev
      - name: Install code-server
        run: curl -fsSL https://code-server.dev/install.sh | sh
      - name: Install Playwright browsers
        working-directory: ./vscode/extension
        run: pnpm exec playwright install
      - name: Run e2e tests
        working-directory: ./vscode/extension
        timeout-minutes: 30
        run: |
          source ../../.venv/bin/activate
          pnpm run test:e2e
      - uses: actions/upload-artifact@v5
        if: ${{ !cancelled() }}
        with:
          name: playwright-report
          path: vscode/extension/playwright-report/
          retention-days: 30
  test-dbt-versions:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        dbt-version: ['1.3', '1.4', '1.5', '1.6', '1.7', '1.8', '1.9', '1.10']
    steps:
      - uses: actions/checkout@v5
      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.10'
      - name: Install uv
        uses: astral-sh/setup-uv@v7
      - name: Install SQLMesh dev dependencies
        run: |
          uv venv .venv
          source .venv/bin/activate
          UV=1 make install-dev-dbt-${{ matrix.dbt-version }}
      - name: Run dbt tests
        # We can't run slow tests across all engines due to tests requiring DuckDB and old versions
        # of DuckDB require a version of DuckDB we no longer support
        run: |
          source .venv/bin/activate

          # Remove semantic_models and metrics sections for DBT versions < 1.6.0
          # Using explicit list to avoid version comparison issues
          if [[ "${{ matrix.dbt-version }}" == "1.3" ]] || \
             [[ "${{ matrix.dbt-version }}" == "1.4" ]] || \
             [[ "${{ matrix.dbt-version }}" == "1.5" ]]; then
            
            echo "DBT version is ${{ matrix.dbt-version }} (< 1.6.0), removing semantic_models and metrics sections..."
            
            schema_file="tests/fixtures/dbt/sushi_test/models/schema.yml"
            if [[ -f "$schema_file" ]]; then
              echo "Modifying $schema_file..."
              
              # Create a temporary file
              temp_file=$(mktemp)
              
              # Use awk to remove semantic_models and metrics sections
              awk '
                /^semantic_models:/ { in_semantic=1; next }
                /^metrics:/ { in_metrics=1; next }
                /^[^ ]/ && (in_semantic || in_metrics) { 
                  in_semantic=0; 
                  in_metrics=0 
                }
                !in_semantic && !in_metrics { print }
              ' "$schema_file" > "$temp_file"
              
              # Move the temp file back
              mv "$temp_file" "$schema_file"
              
              echo "Successfully removed semantic_models and metrics sections"
            else
              echo "Schema file not found at $schema_file, skipping..."
            fi
          else
            echo "DBT version is ${{ matrix.dbt-version }} (>= 1.6.0), keeping semantic_models and metrics sections"
          fi

          make dbt-fast-test
      - name: Test SQLMesh info in sushi_dbt
        working-directory: ./examples/sushi_dbt
        run: |
          source ../../.venv/bin/activate
          sed -i 's/target: in_memory/target: postgres/g' profiles.yml
          if [[ $(echo -e "${{ matrix.dbt-version }}\n1.5.0" | sort -V | head -n1) == "${{ matrix.dbt-version }}" ]] && [[ "${{ matrix.dbt-version }}" != "1.5.0" ]]; then
            echo "DBT version is ${{ matrix.dbt-version }} (< 1.5.0), removing version parameters..."
            sed -i -e 's/, version=1) }}/) }}/g' -e 's/, v=1) }}/) }}/g' models/top_waiters.sql
          else
            echo "DBT version is ${{ matrix.dbt-version }} (>= 1.5.0), keeping version parameters"
          fi

          sqlmesh info --skip-connection
