name: nightly
permissions:
  contents: write
  packages: write

on:
  workflow_dispatch:
  schedule:
    - cron: "4 3 * * *"

jobs:
  check_commits:
    name: Check for New Commits
    runs-on: ubuntu-latest
    outputs:
      has_new_commits: ${{ steps.check.outputs.new_commits }}
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v6
      - name: Check for new commits
        id: check
        env:
          GITHUB_TOKEN: ${{ secrets.PAT }}
        run: |
          NEW_COMMITS=$(git rev-list --count --after="$(date -Iseconds -d '23 hours ago')" ${{ github.sha }})
          echo "new_commits=$NEW_COMMITS" >> $GITHUB_OUTPUT

  build-standalone-nightly:
    needs:
      - check_commits
    if: ${{ needs.check_commits.outputs.has_new_commits > 0 && inputs.release_type != 'none' }}
    uses: ./.github/workflows/build.yml
    secrets: inherit
    with:
      build_type: "nightly"
      flavor: standalone

  publish:
    needs:
      - build-standalone-nightly
    if: ${{ needs.check_commits.outputs.has_new_commits > 0 && inputs.release_type != 'none' }}
    name: publish-nightly
    runs-on: ubuntu-latest
    steps:

      - uses: actions/checkout@v6
        with:
          submodules: recursive

      - name: Install system dependencies
        run: |
          sudo apt update && sudo apt install -y gh apksigner
      - name: Set latest tag
        uses: rickstaa/action-create-tag@v1
        id: tag_creation
        with:
          tag: "latest"
          message: "Automated tag for HEAD commit"
          force_push_tag: true
          github_token: ${{ github.token }}
          tag_exists_error: false

      - name: Generate Changelog
        id: changelog
        uses: requarks/changelog-action@v1
        with:
          token: ${{ github.token }}
          toTag: "nightly"
          fromTag: "latest"
          writeToFile: false

      - name: Make download dir
        run: mkdir ${{ github.workspace }}/temp

      - name: Download artifacts
        uses: actions/download-artifact@v8
        with:
          pattern: android_artifacts_*
          path: ${{ github.workspace }}/temp

      - name: Set release notes
        run: |
          echo "RELEASE_NOTES=Nightly build for the latest development version of the app." >> $GITHUB_ENV

      - name: Delete previous nightly version
        uses: ClementTsang/delete-tag-and-release@v0.4.0
        with:
          tag_name: "nightly"
          delete_release: true
        env:
          GITHUB_TOKEN: ${{ github.token }}

      - name: Get checksum
        id: checksum
        run: |
          file_path=$(find ${{ github.workspace }}/temp -type f -iname "*.apk" | head -n 1)
          if [ -z "$file_path" ]; then
            echo "No APK file found"
            exit 1
          fi
          checksum=$(apksigner verify --print-certs "$file_path" | grep -Po "(?<=SHA-256 digest:) .*" | tr -d "[:blank:]")
          echo "checksum=$checksum" >> $GITHUB_OUTPUT

      - name: Create nightly release
        id: create_release
        uses: softprops/action-gh-release@v3
        with:
          body: |
            ${{ env.RELEASE_NOTES }}

            SHA-256 fingerprints for the 4096-bit signing certificate:
            ```sh
            ${{ steps.checksum.outputs.checksum }}
            ```

            To verify fingerprint:
            ```sh
            apksigner verify --print-certs [path to APK file] | grep SHA-256
            ```

            ### Changelog
            ${{ steps.changelog.outputs.changes }}
          tag_name: nightly
          name: nightly
          draft: false
          prerelease: true
          make_latest: false
          files: |
            ${{ github.workspace }}/temp/**/*.apk
        env:
          GITHUB_TOKEN: ${{ secrets.PAT }}