name: Release

permissions:
  contents: write
  pull-requests: write

on: 
  workflow_dispatch:
    inputs: 
      version: 
        type: string
        required: false
        description: "Version number Eg: 4.2.0"
      publish_nightly_npm:
        type: boolean
        required: false
        default: false
        description: "Publish nightly NPM package"
  schedule:
    - cron: 0 0 * * *

env:
  VERSION: ${{inputs.version != '' && inputs.version || 'nightly'}}
  VERSION_WITH_V: ${{inputs.version != '' && format('v{0}', inputs.version) || 'nightly'}}
  NIGHTLY_NPM: ${{inputs.publish_nightly_npm}}

jobs:
  release-info:
    runs-on: ubuntu-latest
    steps:
    - name: Release info 
      run: |
        echo "VERSION: $VERSION"
        echo "VERSION_WITH_V: $VERSION_WITH_V"
        echo "NIGHTLY_NPM: $NIGHTLY_NPM"
        
  create-release:
    needs: [release-info]
    runs-on: ubuntu-latest
    steps:
    - name: Checkout 
      uses: actions/checkout@v6
      
    - name: Install dependencies
      run: |
        npm ci
        
    - name: Create release notes
      run: |
        chmod +x ./scripts/rz.py
        if [[ "$VERSION" != "nightly" ]]; then
            ./scripts/rz.py create $VERSION
        else
          ./scripts/rz.py create nightly \
            --no-changes \
            --top=":warning: **Nightly build**: This nightly-release may contain experimental features and breaking changes."
        fi

    - name: Delete release tag
      run: |
        if gh release view $VERSION_WITH_V &> /dev/null; then
          gh release delete $VERSION_WITH_V --cleanup-tag --yes
        fi
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        
    - name: Create a new branch for the release
      run: |
        git config user.name "github-actions[bot]"
        git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
        if git show-ref --quiet refs/heads/$VERSION_WITH_V; then
          git branch -D $VERSION_WITH_V
        fi
        if git ls-remote --exit-code --heads origin $VERSION_WITH_V; then
          git push origin -d $VERSION_WITH_V
        fi
        git checkout -b $VERSION_WITH_V
        git push origin $VERSION_WITH_V
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Create the new release
      run: |
          if [[ "$VERSION" != "nightly" ]]; then
            gh release create $VERSION_WITH_V \
              --title "Neutralinojs CLI $VERSION_WITH_V released!" \
              --notes-file .tmprz/release_notes.md
          else
            gh release create nightly \
              --title "Neutralinojs CLI nightly release" \
              --notes-file .tmprz/release_notes.md \
              --prerelease
          fi
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Send a pull request for the modified changelog
      if: ${{ env.VERSION != 'nightly' }}
      run: |
        branch=changelog-update-$VERSION_WITH_V
        git checkout -b $branch
        git add CHANGELOG.md
        git commit -m "Update changelog for $VERSION_WITH_V"
        git push origin $branch
        gh pr create \
        --title "Update changelog after $VERSION_WITH_V release" \
        --body "This pull request updates CHANGELOG.md file after the recent release." \
        --assignee "${{github.actor}}"
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          
    - name: Clean release notes
      run: |
        ./scripts/rz.py cleanup
      
  publish-npm-package:
    needs: [create-release]
    runs-on: ubuntu-latest
    steps:
    - name: Checkout 
      uses: actions/checkout@v6
      
    - uses: actions/setup-node@v6
      with:
        node-version: "24.x"
        registry-url: "https://registry.npmjs.org"
        scope: "@neutralinojs"

    - name: Publish nightly release
      if: ${{ env.VERSION == 'nightly' && env.NIGHTLY_NPM == 'true' }}
      run: |
        vend=nightly-$(date +"%Y%m%d")
        v=$(npm pkg get version | tr -d '"')-$vend
        npm version $v --commit-hooks=false --git-tag-version=false
        npm publish --access=public --tag nightly
      env:
        NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

    - name: Publish main release
      if: ${{ env.VERSION != 'nightly' }}
      run: |
        npm publish --access=public
      env:
        NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  announcement:
    needs: [publish-npm-package]
    runs-on: ubuntu-latest
    steps:
    - name: Send a Discord message
      if: ${{ env.VERSION != 'nightly' }}
      env:
        DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
      uses: Ilshidur/action-discord@master
      with:
        args: |
          Neutralinojs CLI ${{ env.VERSION_WITH_V }} released 🚀
          Changelog: https://github.com/neutralinojs/neutralinojs-cli/releases/tag/${{ env.VERSION_WITH_V }}
