name: Deploy SDK
on:
  release:
    types:
      - published

jobs:
  deploy:
    name: Deploy SDK to Maven Central
    runs-on: ubuntu-latest
    env:
      BW_ACCOUNT_ID: ${{ secrets.BW_ACCOUNT_ID }}
      BW_USERNAME: ${{ secrets.BW_USERNAME }}
      BW_PASSWORD: ${{ secrets.BW_PASSWORD }}
      BW_CLIENT_ID: ${{ secrets.BW_CLIENT_ID }}
      BW_CLIENT_SECRET: ${{ secrets.BW_CLIENT_SECRET }}
      BW_USERNAME_FORBIDDEN: ${{ secrets.BW_USERNAME_FORBIDDEN }}
      BW_PASSWORD_FORBIDDEN: ${{ secrets.BW_PASSWORD_FORBIDDEN }}
      BW_VOICE_APPLICATION_ID: ${{ secrets.BW_VOICE_APPLICATION_ID }}
      BW_MESSAGING_APPLICATION_ID: ${{ secrets.BW_MESSAGING_APPLICATION_ID }}
      BW_NUMBER: ${{ secrets.BW_NUMBER }}
      USER_NUMBER: ${{ secrets.USER_NUMBER }}
      VZW_NUMBER: ${{ secrets.VZW_NUMBER }}
      ATT_NUMBER: ${{ secrets.ATT_NUMBER }}
      T_MOBILE_NUMBER: ${{ secrets.T_MOBILE_NUMBER }}
      BASE_CALLBACK_URL: ${{ secrets.BASE_CALLBACK_URL }}
      PYTHON_VERSION: ${{ matrix.python-version }}
      OPERATING_SYSTEM: ${{ matrix.os }}
      MANTECA_ACTIVE_NUMBER: ${{ secrets.MANTECA_ACTIVE_NUMBER }}
      MANTECA_IDLE_NUMBER: ${{ secrets.MANTECA_IDLE_NUMBER }}
      MANTECA_BASE_URL: ${{ secrets.MANTECA_BASE_URL }}
      MANTECA_APPLICATION_ID: ${{ secrets.MANTECA_APPLICATION_ID }}
      BW_NUMBER_PROVIDER: ${{ secrets.BW_NUMBER_PROVIDER }}

    steps:
      - name: Set Release Version
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/v}" >> $GITHUB_ENV
      
      - name: Checkout
        uses: actions/checkout@v5

      - name: Set up Apache Maven Central
        uses: actions/setup-java@v5
        with:
          distribution: 'adopt'
          java-version: 21
          server-id: 'ossrh'
          server-username: PUBLISHER_USERNAME
          server-password: PUBLISHER_PASSWORD
          gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
          gpg-passphrase: MAVEN_GPG_PASSPHRASE

      - name: Set OpenAPI Generator Version
        run: |
          OPENAPI_VERSION=$(jq .[\"generator-cli\"].version openapitools.json)
          echo "OPENAPI_GENERATOR_VERSION=$OPENAPI_VERSION" >> $GITHUB_ENV
        shell: bash

      - name: Generate SDK with Updated Version
        uses: Bandwidth/generate-sdk-action@v4.0.0
        with:
          openapi-generator-version: ${{ env.OPENAPI_GENERATOR_VERSION }}
          language: java
          additional-properties: --additional-properties=artifactVersion=$RELEASE_VERSION

      - name: Clean SDK
        run: mvn clean

      - name: Set Maven Project Version
        run: mvn versions:set -DnewVersion=$RELEASE_VERSION

      - name: Build Package and Test
        env:
          JAVA_VERSION: 21
          RUNNER_OS: ubuntu-latest
          OPERATING_SYSTEM: ubuntu -latest
          DISTRIBUTION: adopt
        run: |
          mvn -B package --file pom.xml -DskipTests
          npm install -g @stoplight/prism-cli
          prism mock ./bandwidth.yml & (sleep 3; mvn test -Punit-tests)
        shell: bash

      - name: Publish to Apache Maven Central
        run: mvn deploy -DskipTests
        env:
          PUBLISHER_USERNAME: ${{ secrets.PUBLISHER_USERNAME }}
          PUBLISHER_PASSWORD: ${{ secrets.PUBLISHER_PASSWORD }}
          MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}

      - name: Notify Slack
        uses: Bandwidth/build-notify-slack-action@v2
        if: always()
        with:
          job-status: ${{ job.status }}
          slack-bot-token: ${{ secrets.SLACK_BOT_TOKEN }}
          slack-channel: ${{ secrets.SLACK_CHANNEL }}
