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@v6
        with:
          persist-credentials: false
      - name: "Build SDK"
        uses: ./.github/actions/build-sdk
        with:
          board_id: ${{ matrix.board.id }}
          arch: ${{ matrix.board.arch }}
  GenerateDeviceMatrix:
    runs-on: ubuntu-latest
    needs: [ BuildSdk ]
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v6
        with:
          persist-credentials: false
      - id: set-matrix
        run: python3 Buildscripts/gh-generate-device-matrix.py
  BuildFirmware:
    runs-on: ubuntu-latest
    needs: GenerateDeviceMatrix
    strategy:
      matrix: ${{ fromJSON(needs.GenerateDeviceMatrix.outputs.matrix) }}
    steps:
      - uses: actions/checkout@v6
        with:
          persist-credentials: false
      - 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@v6
        with:
          persist-credentials: false
      - 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@v6
      - 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@v6
      - 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@v6
      - 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
