name: build

on:
  push:
    branches:
      - main
    paths:
      - 'module/**'
  pull_request:
    paths:
      - 'module/**'
  workflow_dispatch:

jobs:
  build:
    name: build
    runs-on: ubuntu-latest
    outputs:
      version_tag_exists: ${{ steps.check_tag.outputs.version_tag_exists }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 2
          fetch-tags: true

      - name: Extract Module Info
        id: extract_info
        run: |
          MODULE_VERSION=$(grep -oP '^version=\K.*' module/module.prop)
          BUILD_COUNT=$((1000 + ${{ github.run_number }}))
          ARTIFACT_NAME="TrickyAddonModule-${MODULE_VERSION}-canary-${BUILD_COUNT}"
          echo "MODULE_VERSION=${MODULE_VERSION}" >> $GITHUB_ENV
          echo "ARTIFACT_NAME=${ARTIFACT_NAME}" >> $GITHUB_ENV

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.ARTIFACT_NAME }}
          path: module/
          include-hidden-files: true

      - name: Check if valid to release
        id: check_tag
        run: |
          if [ "${{ github.event_name }}" = "pull_request" ]; then
            echo "version_tag_exists=true" >> $GITHUB_OUTPUT
          else
            VERSION=$(grep '^version=' module/module.prop | sed 's/version=//')
            git fetch --tags
            if git tag | grep -qx "^$VERSION"; then
              echo "version_tag_exists=true" >> $GITHUB_OUTPUT
            else
              echo "version_tag_exists=false" >> $GITHUB_OUTPUT
            fi
          fi

  release:
    name: release
    runs-on: ubuntu-latest
    needs: build
    if: ${{ needs.build.outputs.version_tag_exists == 'false' }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 2

      - name: Configure Git user
        run: |
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          git config --global user.name "github-actions[bot]"

      - name: Create a new tag
        run: |
          VERSION=$(grep '^version=' module/module.prop | sed 's/version=//')
          git tag -a "$VERSION" -m "Release version $VERSION"
          git push origin "$VERSION"

      - name: Set release variables
        id: set_release_variables
        run: |
          VERSION=$(grep '^version=' module/module.prop | sed 's/version=//')
          CURRENT_TAG="$VERSION"
          RELEASE_NOTES=$(awk -v tag="### $CURRENT_TAG" '
            $0 == tag {flag=1; next}
            /^###/ && flag {exit}
            flag {print}
          ' changelog.md)

          if [[ "$VERSION" == *"beta"* ]]; then
            PRERELEASE=true
            FORMATTED_RELEASE_NOTES=$(echo -e "- Due to extensive code refactoring, you might encounter unexpected bugs in this version, feedback in [Telegram](https://t.me/kowchannel) or [create issue](https://github.com/KOWX712/Tricky-Addon-Update-Target-List/issues) if you found any issues.\n---\n### Tricky Addon module\n$RELEASE_NOTES")
            FILES="TrickyAddonModule-${VERSION}.zip"
          else
            PRERELEASE=false
            FORMATTED_RELEASE_NOTES=$(echo -e "### Tricky Addon module\n$RELEASE_NOTES\n")
            FILES="TrickyAddonModule-${VERSION}.zip"
          fi

          echo "CURRENT_TAG=$CURRENT_TAG" >> $GITHUB_ENV
          echo "ZIP_NAME=TrickyAddonModule-${VERSION}.zip" >> $GITHUB_ENV
          echo "RELEASE_NOTES<<EOF" >> $GITHUB_ENV
          echo "$FORMATTED_RELEASE_NOTES" >> $GITHUB_ENV
          echo "EOF" >> $GITHUB_ENV
          echo "FILES=$FILES" >> $GITHUB_ENV
          echo "PRERELEASE=$PRERELEASE" >> $GITHUB_ENV

      - name: Compress module folder
        run: |
          cd module && zip -r "../${{ env.ZIP_NAME }}" ./*
          echo "Created zip file: ${{ env.ZIP_NAME }}"

      - name: Create release
        uses: softprops/action-gh-release@v2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          files: ${{ env.FILES }}
          tag_name: ${{ env.CURRENT_TAG }}
          name: ${{ env.CURRENT_TAG }}
          body: ${{ env.RELEASE_NOTES }}
          draft: false
          prerelease: ${{ env.PRERELEASE }}