name: Setup Build Environment
description: Setup Build Environment
runs:
  using: "composite"
  steps:
    - name: Restore uv Cache
      uses: actions/cache@v5
      with:
        path: |
          ~/.cache/uv
          .venv
        key: ${{ runner.os }}-py311-uv-${{ hashFiles('uv.lock', 'pyproject.toml') }}
        restore-keys: |
          ${{ runner.os }}-py311-uv-

    - name: Restore PlatformIO Cache
      uses: actions/cache@v5
      with:
        path: |
          ~/.platformio/.cache
          ~/.platformio/packages
          ~/.platformio/platforms
          ~/.platformio/lib
        key: ${{ runner.os }}-pio-${{ hashFiles('platformio.ini', 'variants/**/platformio.ini') }}
        restore-keys: |
          ${{ runner.os }}-pio-

    - name: Install Python
      uses: actions/setup-python@v6
      with:
        python-version: "3.11"

    - name: Install uv
      uses: astral-sh/setup-uv@v8.0.0

    - name: Sync Python Tooling
      shell: bash
      run: |
        uv sync --locked --no-group docs
        uv venv --allow-existing --seed
        echo "$GITHUB_WORKSPACE/.venv/bin" >> "$GITHUB_PATH"

    # Active release tag formats:
    #   companion-wifi-v1.2.3
    #   repeater-mqtt-eastmesh-v1.0.1
    # with OFFICIAL_MESHCORE_VERSION=v1.2.3 configured as a GitHub variable.
    #
    # This exports:
    #   companion-wifi-v1.2.3 -> FIRMWARE_VERSION=v1.2.3, RELEASE_VERSION=v1.2.3
    #   repeater-mqtt-eastmesh-v1.0.1 -> FIRMWARE_VERSION=v1.2.3, EASTMESH_VERSION=v1.0.1,
    #                                    RELEASE_VERSION=v1.2.3-eastmesh-v1.0.1
    - name: Extract Version Info
      shell: bash
      run: |
        STATIC_OFFICIAL_VERSION="${OFFICIAL_MESHCORE_VERSION:-}"

        if [[ "$GITHUB_REF" != refs/tags/* ]]; then
          echo "GIT_TAG_NAME=" >> "$GITHUB_ENV"
          echo "GIT_TAG_VERSION=" >> "$GITHUB_ENV"
          echo "OFFICIAL_MESHCORE_VERSION=$STATIC_OFFICIAL_VERSION" >> "$GITHUB_ENV"
          echo "EASTMESH_VERSION=" >> "$GITHUB_ENV"
          echo "FIRMWARE_VERSION=$STATIC_OFFICIAL_VERSION" >> "$GITHUB_ENV"
          echo "RELEASE_VERSION=$STATIC_OFFICIAL_VERSION" >> "$GITHUB_ENV"
          exit 0
        fi

        GIT_TAG_NAME="${GITHUB_REF#refs/tags/}"
        EASTMESH_VERSION=""
        FIRMWARE_VERSION="$STATIC_OFFICIAL_VERSION"

        if [[ "$GIT_TAG_NAME" =~ ^repeater-mqtt-eastmesh-(v[[:alnum:]._-]+)$ ]]; then
          if [[ -z "$STATIC_OFFICIAL_VERSION" ]]; then
            echo "OFFICIAL_MESHCORE_VERSION must be set for EastMesh release tags" >&2
            exit 1
          fi
          EASTMESH_VERSION="${BASH_REMATCH[1]}"
        elif [[ "$GIT_TAG_NAME" =~ ^companion-wifi-(v[[:alnum:]._-]+)$ ]]; then
          FIRMWARE_VERSION="${BASH_REMATCH[1]}"
        else
          echo "Unsupported release tag format: $GIT_TAG_NAME" >&2
          exit 1
        fi

        if [[ -n "$STATIC_OFFICIAL_VERSION" && "$FIRMWARE_VERSION" != "$STATIC_OFFICIAL_VERSION" ]]; then
          echo "Tag official version '$FIRMWARE_VERSION' does not match OFFICIAL_MESHCORE_VERSION '$STATIC_OFFICIAL_VERSION'" >&2
          exit 1
        fi

        RELEASE_VERSION="$FIRMWARE_VERSION"
        if [[ -n "$EASTMESH_VERSION" ]]; then
          RELEASE_VERSION="${RELEASE_VERSION}-eastmesh-${EASTMESH_VERSION}"
        fi

        echo "GIT_TAG_NAME=$GIT_TAG_NAME" >> "$GITHUB_ENV"
        echo "GIT_TAG_VERSION=$RELEASE_VERSION" >> "$GITHUB_ENV"
        echo "OFFICIAL_MESHCORE_VERSION=$FIRMWARE_VERSION" >> "$GITHUB_ENV"
        echo "EASTMESH_VERSION=$EASTMESH_VERSION" >> "$GITHUB_ENV"
        echo "FIRMWARE_VERSION=$FIRMWARE_VERSION" >> "$GITHUB_ENV"
        echo "RELEASE_VERSION=$RELEASE_VERSION" >> "$GITHUB_ENV"
