name: CD

on:
  release:
    types:
      - published

jobs:
  # Pre-cleanup: delete all existing assets from the release to avoid gh release upload failure
  pre-release-cleanup:
    runs-on: ubuntu-latest
    steps:
      - name: Delete Existing Release Assets
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          TAG_NAME="${{ github.event.release.tag_name }}"
          # Capture asset list first to avoid pipefail issues
          ASSETS=$(gh release view "$TAG_NAME" --json assets --jq '.assets[].name' 2>/dev/null || true)
          if [ -n "$ASSETS" ]; then
            echo "$ASSETS" | while read -r filename; do
              echo "Deleting existing asset: $filename"
              gh release delete-asset "$TAG_NAME" "$filename" --yes 2>/dev/null || true
            done
          else
            echo "No existing assets to delete"
          fi
        shell: bash

  cd:
    needs: pre-release-cleanup
    uses: halo-sigs/reusable-workflows/.github/workflows/plugin-cd.yaml@v4
    permissions:
      contents: write
    with:
      ui-path: "ui"
      pnpm-version: 10
      node-version: 24
      java-version: 21
      # Remove skip-appstore-release and set app-id if you want to release to the App Store
      skip-appstore-release: true
      # app-id: app-xyz
