name: Build Boards

on:
  push:
    branches:
      - main
      - ci/* # for ci test
  pull_request:
    branches:
      - main

permissions:
  contents: read

jobs:
  prepare:
    name: Determine variants to build
    runs-on: ubuntu-latest
    container:
      image: espressif/idf:v6.0.1
    outputs:
      variants: ${{ steps.select.outputs.variants }}
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Test release tooling
        run: python -m unittest discover -s scripts/tests -v

      - id: list
        name: Get all variant list
        run: |
          echo "all_variants=$(python scripts/release.py --list-boards --json)" >> $GITHUB_OUTPUT

      - id: select
        name: Select variants based on changes
        shell: bash
        env:
          ALL_VARIANTS: ${{ steps.list.outputs.all_variants }}
        run: |
          EVENT_NAME="${{ github.event_name }}"

          # push 到 main 分支，编译全部变体
          if [[ "$EVENT_NAME" == "push" ]]; then
            echo "variants=$ALL_VARIANTS" >> $GITHUB_OUTPUT
            exit 0
          fi

          # pull_request 场景
          # actions/checkout checks out GitHub's synthetic PR merge commit. Its
          # first and second parents are the exact base and head commits used
          # for this run, even when the event SHAs are not exposed as refs in
          # the job container.
          git config --global --add safe.directory "$GITHUB_WORKSPACE"
          BASE_SHA="HEAD^1"
          HEAD_SHA="HEAD^2"
          echo "Base: $(git rev-parse "$BASE_SHA"), Head: $(git rev-parse "$HEAD_SHA")"

          CHANGED=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA")
          echo -e "Changed files:\n$CHANGED"

          SELECTED=$(printf '%s\n' "$CHANGED" | python scripts/release.py --select-changed)
          echo "variants=$SELECTED" >> $GITHUB_OUTPUT

  build:
    name: Build ${{ matrix.full_name }}
    needs: prepare
    if: ${{ needs.prepare.outputs.variants != '[]' }}
    strategy:
      fail-fast: false  # 单个变体失败不影响其它变体
      matrix:
        include: ${{ fromJson(needs.prepare.outputs.variants) }}
    runs-on: ubuntu-latest
    container:
      image: espressif/idf:v6.0.1
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Build current variant
        shell: bash
        run: |
          source $IDF_PATH/export.sh
          python scripts/release.py ${{ matrix.board }} --name ${{ matrix.name }}

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: xiaozhi_${{ matrix.full_name }}_${{ github.sha }}
          path: build/merged-binary.bin
          if-no-files-found: error

  compatibility-idf5:
    name: IDF 5.5 compatibility - ${{ matrix.name }}
    if: ${{ github.event_name == 'pull_request' }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - board: xmini-c3
            name: xmini-c3
          - board: esp-p4-function-ev-board
            name: esp-p4-function-ev-board
    runs-on: ubuntu-latest
    container:
      image: espressif/idf:v5.5.4
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Build compatibility variant
        shell: bash
        run: |
          source $IDF_PATH/export.sh
          python scripts/release.py ${{ matrix.board }} --name ${{ matrix.name }}

  blufi-idf6:
    name: IDF 6 BluFi option
    runs-on: ubuntu-latest
    container:
      image: espressif/idf:v6.0.1
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Build BluFi-enabled firmware
        shell: bash
        run: |
          source $IDF_PATH/export.sh
          idf.py -B build-blufi \
            -DSDKCONFIG=build-blufi/sdkconfig \
            -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.defaults.esp32s3;scripts/ci/blufi.sdkconfig.defaults" \
            -DIDF_TARGET=esp32s3 \
            -DBOARD_NAME=bread-compact-wifi \
            -DBOARD_TYPE=bread-compact-wifi \
            build
