name: Bump App Version
on: 
  workflow_dispatch:
    inputs:
      type:
        description: 'Bump type'
        required: true
        default: 'patch'
        type: choice
        options:
        - patch
        - minor
        - major
      releaseNote:
        description: 'Release Note'
        required: true
        type: string

jobs:
  bump:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      # - name: Checkout
      #   run: |
      #       git checkout -b autoVersionBump

      - name: 🏗 Bump App Version
        id: bump-app-version
        run: |
          newVersion=$(bash ./scripts/versionBump.sh $TYPE "$RELEASE_NOTES")
          echo "::newVersion::$newVersion"
          echo "newVersion=$newVersion" >> "$GITHUB_OUTPUT"
        env:
          TYPE: ${{ inputs.type }}
          RELEASE_NOTES: ${{ inputs.releaseNote }}
        
        
      - name: Git Add and Commit
        run: |
            git config --global user.name 'Yogesh Choudhary Paliyal'
            git config --global user.email 'yogeshpaliyal@users.noreply.github.com'
            git add .
            git commit -am "Github Actions: App version Bumped to ${{ steps.bump-app-version.outputs.newVersion }}"

      - name: Print newVersion
        run: echo "${{ steps.bump-app-version.outputs.newVersion }}"

      - name: Push to protected branch
        uses: CasperWA/push-protected@v2
        with:
          token: ${{ secrets.PUSH_TO_PROTECTED_BRANCH }}
          branch: master
          unprotect_reviews: true

      - name: Git push tag
        run: |
            git tag -a ${{ steps.bump-app-version.outputs.newVersion }} -m "Release version ${{ steps.bump-app-version.outputs.newVersion }}"
            git push origin ${{ steps.bump-app-version.outputs.newVersion }}


      - name: Create Release
        uses: ncipollo/release-action@v1.13.0
        with:
          tag: "${{ steps.bump-app-version.outputs.newVersion }}"
          generateReleaseNotes: true
          commit: "master"
          discussionCategory: "Release feedbacks"
          makeLatest: true

      # - name: Create Pull Request
      #   uses: peter-evans/create-pull-request@v5.0.0

