name: Release Firmware

on:
  push:
    branches: [main]
    tags: ['*']

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - name: esp32
            target: esp32
            led_builtin: 2
            bsp_config_gpio: 9
            board_specific_init: boards/default.h
          - name: xiao-esp32c6
            target: esp32c6
            led_builtin: 8
            bsp_config_gpio: 0
            board_specific_init: boards/xiao_esp32c6.h

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Build firmware (${{ matrix.name }})
        run: |
          docker build \
            --build-arg TARGET=${{ matrix.target }} \
            --build-arg LED_BUILTIN=${{ matrix.led_builtin }} \
            --build-arg BSP_CONFIG_GPIO=${{ matrix.bsp_config_gpio }} \
            --build-arg BOARD_SPECIFIC_INIT=${{ matrix.board_specific_init }} \
            -t esp-scope-${{ matrix.name }} .

      - name: Extract binaries (${{ matrix.name }})
        run: |
          cid=$(docker create esp-scope-${{ matrix.name }})
          docker cp "$cid:/workspace/build/merged-firmware.bin"            esp-scope-${{ matrix.name }}-merged.bin
          docker cp "$cid:/workspace/build/bootloader/bootloader.bin"      esp-scope-${{ matrix.name }}-bootloader.bin
          docker cp "$cid:/workspace/build/partition_table/partition-table.bin" esp-scope-${{ matrix.name }}-partition-table.bin
          docker cp "$cid:/workspace/build/esp-scope.bin"                  esp-scope-${{ matrix.name }}-app.bin
          docker rm "$cid"

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: esp-scope-${{ matrix.name }}
          path: esp-scope-${{ matrix.name }}-*.bin

  release:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4

      - name: Set release tag
        id: tag
        run: |
          if [[ "${GITHUB_REF}" == refs/tags/* ]]; then
            echo "tag=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT"
            echo "prerelease=false" >> "$GITHUB_OUTPUT"
          else
            echo "tag=latest" >> "$GITHUB_OUTPUT"
            echo "prerelease=true" >> "$GITHUB_OUTPUT"
          fi

      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.tag.outputs.tag }}
          prerelease: ${{ steps.tag.outputs.prerelease }}
          body: |
            ## Firmware binaries

            Each board ships four files:

            | Suffix | Contents | Use for |
            |--------|----------|---------|
            | `-merged.bin` | Bootloader + partition table + app in one file | Web installer, quick CLI flash |
            | `-bootloader.bin` | Bootloader only | Individual flashing |
            | `-partition-table.bin` | Partition table only | Individual flashing |
            | `-app.bin` | Application only | OTA / app-only update |

            ### Generic ESP32 — LED GPIO 2 · AP-mode button GPIO 9

            ```bash
            # Flash merged binary (simplest)
            esptool.py -p <PORT> -b 460800 write_flash 0x0 esp-scope-esp32-merged.bin

            # Or flash components individually
            esptool.py -p <PORT> -b 460800 write_flash \
              0x1000  esp-scope-esp32-bootloader.bin \
              0x8000  esp-scope-esp32-partition-table.bin \
              0x10000 esp-scope-esp32-app.bin
            ```

            ### Seeed XIAO ESP32C6 — LED GPIO 8 (built-in yellow) · AP-mode button GPIO 0 (Boot)

            ```bash
            # Flash merged binary (simplest)
            esptool.py -p <PORT> -b 460800 write_flash 0x0 esp-scope-xiao-esp32c6-merged.bin

            # Or flash components individually
            esptool.py -p <PORT> -b 460800 write_flash \
              0x0     esp-scope-xiao-esp32c6-bootloader.bin \
              0x8000  esp-scope-xiao-esp32c6-partition-table.bin \
              0x10000 esp-scope-xiao-esp32c6-app.bin
            ```

            Replace `<PORT>` with your serial port (e.g. `/dev/ttyUSB0` or `COM3`).

            ## Web installer

            Flash directly from your browser (Chrome/Edge/Opera) — no tools needed:

            👉 **[https://vtalpaert.github.io/esp-scope/](https://vtalpaert.github.io/esp-scope/)**
          files: |
            esp-scope-esp32/esp-scope-esp32-merged.bin
            esp-scope-esp32/esp-scope-esp32-bootloader.bin
            esp-scope-esp32/esp-scope-esp32-partition-table.bin
            esp-scope-esp32/esp-scope-esp32-app.bin
            esp-scope-xiao-esp32c6/esp-scope-xiao-esp32c6-merged.bin
            esp-scope-xiao-esp32c6/esp-scope-xiao-esp32c6-bootloader.bin
            esp-scope-xiao-esp32c6/esp-scope-xiao-esp32c6-partition-table.bin
            esp-scope-xiao-esp32c6/esp-scope-xiao-esp32c6-app.bin
