name: Build Firmware
on:
  push:
    branches:
      - main
    tags:
      - v*
  pull_request:
    types: [ opened, synchronize, reopened ]

permissions: read-all

jobs:
  BuildSdk:
    strategy:
      matrix:
        board: [
          { id: generic-esp32, arch: esp32 },
          { id: generic-esp32c6, arch: esp32c6 },
          { id: generic-esp32p4, arch: esp32p4 },
          { id: generic-esp32s3, arch: esp32s3 },
        ]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: "Build SDK"
        uses: ./.github/actions/build-sdk
        with:
          board_id: ${{ matrix.board.id }}
          arch: ${{ matrix.board.arch }}
  BuildFirmware:
    strategy:
      matrix:
        board: [
          { id: btt-panda-touch, arch: esp32s3 },
          { id: cyd-2432s024c, arch: esp32 },
          { id: cyd-2432s028r, arch: esp32 },
          { id: cyd-2432s028rv3, arch: esp32 },
          { id: cyd-e32r28t, arch: esp32 },
          { id: cyd-e32r32p, arch: esp32 },
          { id: cyd-2432s032c, arch: esp32 },
          { id: cyd-8048s043c, arch: esp32s3 },
          { id: cyd-4848s040c, arch: esp32s3 },
          { id: elecrow-crowpanel-advance-28, arch: esp32s3 },
          { id: elecrow-crowpanel-advance-35, arch: esp32s3 },
          { id: elecrow-crowpanel-advance-50, arch: esp32s3 },
          { id: elecrow-crowpanel-basic-28, arch: esp32 },
          { id: elecrow-crowpanel-basic-35, arch: esp32 },
          { id: elecrow-crowpanel-basic-50, arch: esp32s3 },
          { id: guition-jc1060p470ciwy, arch: esp32p4 },
          { id: guition-jc2432w328c, arch: esp32 },
          { id: guition-jc3248w535c, arch: esp32s3 },
          { id: guition-jc8048w550c, arch: esp32s3 },
          { id: heltec-wifi-lora-32-v3, arch: esp32s3 },
          { id: lilygo-tdeck, arch: esp32s3 },
          { id: lilygo-tdongle-s3, arch: esp32s3 },
          { id: lilygo-tdisplay-s3, arch: esp32s3 },
          { id: lilygo-tlora-pager, arch: esp32s3 },
          { id: lilygo-tdisplay, arch: esp32 },
          { id: m5stack-cardputer, arch: esp32s3 },
          { id: m5stack-cardputer-adv, arch: esp32s3 },
          { id: m5stack-core2, arch: esp32 },
          { id: m5stack-cores3, arch: esp32s3 },
          { id: m5stack-papers3, arch: esp32s3 },
          { id: m5stack-stickc-plus, arch: esp32 },
          { id: m5stack-stickc-plus2, arch: esp32 },
          { id: m5stack-tab5, arch: esp32p4 },
          { id: unphone, arch: esp32s3 },
          { id: waveshare-esp32-s3-geek, arch: esp32s3 },
          { id: waveshare-s3-lcd-13, arch: esp32s3 },
          { id: waveshare-s3-touch-lcd-128, arch: esp32s3 },
          { id: waveshare-s3-touch-lcd-147, arch: esp32s3 },
          { id: waveshare-s3-touch-lcd-43, arch: esp32s3 },
          { id: wireless-tag-wt32-sc01-plus, arch: esp32s3 }
        ]
    runs-on: ubuntu-latest
    needs: [ BuildSdk ]
    steps:
      - uses: actions/checkout@v4
      - name: "Build Firmware"
        uses: ./.github/actions/build-firmware
        with:
          board_id: ${{ matrix.board.id }}
          arch: ${{ matrix.board.arch }}
  BundleArtifacts:
    runs-on: ubuntu-latest
    needs: [ BuildFirmware ]
    if: |
      (github.event_name == 'push' && github.ref == 'refs/heads/main') ||
      (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v'))
    steps:
      - uses: actions/checkout@v4
      - name: "Bundle Artifacts"
        uses: ./.github/actions/bundle-artifacts
  PublishFirmwareSnapshot:
    runs-on: ubuntu-latest
    needs: [ BundleArtifacts ]
    if: (github.event_name == 'push' && github.ref == 'refs/heads/main')
    steps:
      - uses: actions/checkout@v4
      - name: "Publish Firmware Snapshot"
        env:
          CDN_ID: ${{ secrets.CDN_ID }}
          CDN_TOKEN_NAME: ${{ secrets.CDN_TOKEN_NAME }}
          CDN_TOKEN_VALUE: ${{ secrets.CDN_TOKEN_VALUE }}
        uses: ./.github/actions/publish-firmware
        with:
          cdn_version: snapshot
  PublishFirmwareStable:
    runs-on: ubuntu-latest
    needs: [ BundleArtifacts ]
    if: (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v'))
    steps:
      - uses: actions/checkout@v4
      - name: "Publish Firmware Stable"
        env:
          CDN_ID: ${{ secrets.CDN_ID }}
          CDN_TOKEN_NAME: ${{ secrets.CDN_TOKEN_NAME }}
          CDN_TOKEN_VALUE: ${{ secrets.CDN_TOKEN_VALUE }}
        uses: ./.github/actions/publish-firmware
        with:
          cdn_version: stable
  PublishSdk:
    runs-on: ubuntu-latest
    needs: [ BundleArtifacts ]
    if: (github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')))
    steps:
      - uses: actions/checkout@v4
      - name: "Publish SDKs"
        env:
          CDN_ID: ${{ secrets.CDN_ID }}
          CDN_TOKEN_NAME: ${{ secrets.CDN_TOKEN_NAME }}
          CDN_TOKEN_VALUE: ${{ secrets.CDN_TOKEN_VALUE }}
        uses: ./.github/actions/publish-sdk
