name: autofix.ci
on:
  pull_request:
    paths:
      - "**/*.py"
      - "src/lfx/src/lfx/components/**"
      - "scripts/build_component_index.py"
    types: [opened, synchronize, reopened, labeled]
env:
  PYTHON_VERSION: "3.13"


jobs:
  lint:
    name: Run Ruff Check and Format
    if: github.event.action != 'labeled' && github.actor != 'github-actions[bot]'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: "Setup Environment"
        uses: astral-sh/setup-uv@v6
        with:
          enable-cache: true
          cache-dependency-glob: "uv.lock"
          python-version: ${{ env.PYTHON_VERSION }}
          prune-cache: false
      - run: uv run ruff check --fix-only .
      - run: uv run ruff format . --config pyproject.toml
      - uses: autofix-ci/action@635ffb0c9798bd160680f18fd73371e355b85f27


  update-starter-projects:
    name: Update Starter Projects
    if: github.event.action != 'labeled' && github.actor != 'github-actions[bot]'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: "Setup Environment"
        uses: astral-sh/setup-uv@v6
        with:
          enable-cache: true
          cache-dependency-glob: "uv.lock"
          python-version: ${{ env.PYTHON_VERSION }}
          prune-cache: false

      - name: "Install dependencies"
        run: |
          uv sync
          uv pip install -e .

      - name: Run starter projects update
        run: uv run python scripts/ci/update_starter_projects.py

      - uses: autofix-ci/action@635ffb0c9798bd160680f18fd73371e355b85f27


  update-component-index:
    name: Update Component Index
    if: >-
      github.actor != 'github-actions[bot]'
      && (github.event.action != 'labeled' || github.event.label.name == 'fix-index')
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          repository: ${{ github.event.pull_request.head.repo.full_name }}
          ref: ${{ github.event.pull_request.head.ref }}
          fetch-depth: 0
      - name: Merge base branch
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git merge origin/${{ github.base_ref }} --no-edit || {
            # Check which files have conflicts
            CONFLICTED=$(git diff --name-only --diff-filter=U)

            # If any non-generated files conflict, bail out — needs manual resolution
            if echo "$CONFLICTED" | grep -v component_index.json | grep -q .; then
              echo "::error::Merge conflicts in non-generated files require manual resolution"
              git merge --abort
              exit 1
            fi

            # Only component_index.json conflicts — resolve and finish merge
            git checkout --theirs src/lfx/src/lfx/_assets/component_index.json
            git add src/lfx/src/lfx/_assets/component_index.json
            GIT_EDITOR=true git merge --continue
          }
      - name: "Setup Environment"
        uses: astral-sh/setup-uv@v6
        with:
          enable-cache: true
          cache-dependency-glob: "uv.lock"
          python-version: ${{ env.PYTHON_VERSION }}
          prune-cache: false

      - name: "Install dependencies"
        run: uv sync --dev --project .

      - name: Build component index
        env:
          LFX_DEV: "1"
        run: make build_component_index

      - uses: autofix-ci/action@635ffb0c9798bd160680f18fd73371e355b85f27
