name: Publish Core Libraries

on:
  release:
    types: [created]
  workflow_dispatch:
    inputs:
      version_suffix:
        description: 'Version suffix (e.g. -alpha01, -SNAPSHOT)'
        required: false
        default: '-SNAPSHOT'

jobs:
  publish:
    runs-on: ubuntu-24.04
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          submodules: 'recursive'

      - name: Gradle Setup
        uses: ./.github/actions/gradle-setup
        with:
          gradle_encryption_key: ${{ secrets.GRADLE_ENCRYPTION_KEY }}

      - name: Configure Version
        id: version
        env:
          EVENT_NAME: ${{ github.event_name }}
          RELEASE_TAG: ${{ github.event.release.tag_name }}
          VERSION_SUFFIX: ${{ inputs.version_suffix }}
        run: |
          if [[ "$EVENT_NAME" == "release" ]]; then
            echo "VERSION_NAME=$RELEASE_TAG" >> $GITHUB_ENV
          else
             # Use a timestamp-based version for manual/branch builds to avoid collisions
             # or use the base version + suffix
             BASE_VERSION=$(grep "VERSION_NAME_BASE" config.properties | cut -d'=' -f2)
             echo "VERSION_NAME=${BASE_VERSION}${VERSION_SUFFIX}" >> $GITHUB_ENV
          fi

      - name: Publish to GitHub Packages
        run: ./gradlew :core:api:publish :core:model:publish :core:proto:publish
        env:
          GITHUB_ACTOR: ${{ github.actor }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
