name: PlatformIO Build

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

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install PlatformIO
        run: pip install platformio esptool

      - name: Build firmware
        run: pio run -e ratdeck_915

      - name: Merge single .bin for M5Burner
        run: |
          esptool.py --chip esp32s3 merge_bin \
            --flash_mode qio --flash_size 16MB \
            -o ratdeck-merged.bin \
            0x0000 .pio/build/ratdeck_915/bootloader.bin \
            0x8000 .pio/build/ratdeck_915/partitions.bin \
            0xe000 ~/.platformio/packages/framework-arduinoespressif32/tools/partitions/boot_app0.bin \
            0x10000 .pio/build/ratdeck_915/firmware.bin

      - name: Package firmware ZIP
        run: |
          mkdir -p ratdeck-firmware
          cp .pio/build/ratdeck_915/bootloader.bin ratdeck-firmware/
          cp .pio/build/ratdeck_915/partitions.bin ratdeck-firmware/
          cp ~/.platformio/packages/framework-arduinoespressif32/tools/partitions/boot_app0.bin ratdeck-firmware/
          cp .pio/build/ratdeck_915/firmware.bin ratdeck-firmware/
          cp ratdeck-merged.bin ratdeck-firmware/
          cat > ratdeck-firmware/manifest.json << 'MANIFEST'
          {
            "chipFamily": "ESP32-S3",
            "flashSize": "16MB",
            "flashMode": "qio",
            "flashFreq": "80m",
            "parts": [
              { "path": "bootloader.bin", "offset": "0x0000" },
              { "path": "partitions.bin", "offset": "0x8000" },
              { "path": "boot_app0.bin", "offset": "0xe000" },
              { "path": "firmware.bin", "offset": "0x10000" }
            ]
          }
          MANIFEST
          cd ratdeck-firmware && zip -r ../ratdeck-firmware.zip . && cd ..

      - name: Upload firmware artifact
        uses: actions/upload-artifact@v4
        with:
          name: ratdeck-firmware
          path: |
            ratdeck-firmware.zip
            ratdeck-merged.bin

  release:
    needs: build
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install PlatformIO
        run: pip install platformio esptool

      - name: Build firmware
        run: pio run -e ratdeck_915

      - name: Merge single .bin for M5Burner
        run: |
          esptool.py --chip esp32s3 merge_bin \
            --flash_mode qio --flash_size 16MB \
            -o ratdeck-merged.bin \
            0x0000 .pio/build/ratdeck_915/bootloader.bin \
            0x8000 .pio/build/ratdeck_915/partitions.bin \
            0xe000 ~/.platformio/packages/framework-arduinoespressif32/tools/partitions/boot_app0.bin \
            0x10000 .pio/build/ratdeck_915/firmware.bin

      - name: Package firmware ZIP
        run: |
          mkdir -p ratdeck-firmware
          cp .pio/build/ratdeck_915/bootloader.bin ratdeck-firmware/
          cp .pio/build/ratdeck_915/partitions.bin ratdeck-firmware/
          cp ~/.platformio/packages/framework-arduinoespressif32/tools/partitions/boot_app0.bin ratdeck-firmware/
          cp .pio/build/ratdeck_915/firmware.bin ratdeck-firmware/
          cp ratdeck-merged.bin ratdeck-firmware/
          cat > ratdeck-firmware/manifest.json << 'MANIFEST'
          {
            "chipFamily": "ESP32-S3",
            "flashSize": "16MB",
            "flashMode": "qio",
            "flashFreq": "80m",
            "parts": [
              { "path": "bootloader.bin", "offset": "0x0000" },
              { "path": "partitions.bin", "offset": "0x8000" },
              { "path": "boot_app0.bin", "offset": "0xe000" },
              { "path": "firmware.bin", "offset": "0x10000" }
            ]
          }
          MANIFEST
          cd ratdeck-firmware && zip -r ../ratdeck-firmware.zip . && cd ..

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            ratdeck-firmware.zip
            ratdeck-merged.bin
          generate_release_notes: true
