name: FW Build / Test

on: [push, pull_request]

jobs:
  determine-builds:
    name: Evaluate Required Builds
    runs-on: ubuntu-20.04
    timeout-minutes: 1
    # Map a step output to job output
    outputs:
      arm-tag_newton-m3-nrf52811: ${{ steps.filter.outputs.arm-tag_newton-m3-nrf52811 }}
      arm-tag_nrf-bootloader: ${{ steps.filter.outputs.arm-tag_nrf-bootloader }}
      arm-tag_nrf52811-pio-ap: ${{ steps.filter.outputs.arm-tag_nrf52811-pio-ap }}
      arm-tag_88MZ100: ${{ steps.filter.outputs.arm-tag_88MZ100 }}
      esp32-ap: ${{ steps.filter.outputs.esp32-ap }}
    steps:
      - uses: actions/checkout@v4
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          # TODO: work around issue with paths-filter (https://github.com/dorny/paths-filter/issues/227)
          token: ''
          filters: .github/path-filters.yml

  tag-build:
    name: Build Tag FW
    needs: [determine-builds]
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout Code (with submodules)
        uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - uses: ./.github/actions/setup-pio

      - name: ARM_Tag_FW/nRF_Bootloader
        if: ${{ needs.determine-builds.outputs.arm-tag_nrf-bootloader == 'true' }}
        run: |
          cd ARM_Tag_FW/nRF_Bootloader
          pio run --environment nRFBootload

      - name: ARM_Tag_FW/Newton_M3_nRF52811
        if: ${{ needs.determine-builds.outputs.arm-tag_newton-m3-nrf52811 == 'true' }}
        run: |
          pio --version
          cd ARM_Tag_FW/Newton_M3_nRF52811
          pio run --environment Newton_M3_Universal

      - name: ARM_Tag_FW/nrf52811_Platformio_AP
        if: ${{ needs.determine-builds.outputs.arm-tag_nrf52811-pio-ap == 'true' }}
        run: |
          cd ARM_Tag_FW/nrf52811_Platformio_AP
          pio run --environment nrf52811_OEPL_AP

      - name: ARM_Tag_FW/88MZ100_OpenEpaperLink_7.4
        if: ${{ needs.determine-builds.outputs.arm-tag_88MZ100 == 'true' }}
        # TODO: Implement build for 88MZ100
        run: |
          echo "Note: build for 88MZ100 has not been implementted yet."

  ap-build:
    name: Build AP FW
    needs: [determine-builds]
    if: ${{ needs.determine-builds.outputs.esp32-ap == 'true' }}
    strategy:
      matrix:
        environment:
          #- OpenEPaperLink_Mini_AP
          #- OpenEPaperLink_AP_and_Flasher
          - ESP32_S3_16_8_YELLOW_AP
          - OpenEPaperLink_Mini_AP_v4
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
      - uses: ./.github/actions/setup-pio

      - name: Build ${{ matrix.environment }}
        run: |
          cd ESP32_AP-Flasher
          pio run --environment ${{ matrix.environment }}
          pio run --target buildfs --environment ${{ matrix.environment }}


