# #
#   @type               github workflow
#   @author             Aetherinox
#   @url                https://github.com/Aetherinox
#   @usage              builds mkdocs from the main branch /docs/ folder and puts the compiled version
#                       in the `gh-pages` branch. Is hosted using Github Pages.
#
#   @update             use the following commands to update mkdocs and the mkdocs-material theme:
#                           pip install --upgrade mkdocs
#                           pip install --upgrade --force-reinstall mkdocs-material
#
#   @secrets            secrets.SELF_TOKEN                                          self github personal access token (fine-grained)
#                       secrets.SELF_TOKEN_CL                                       self github personal access token (classic)
#                       secrets.NPM_TOKEN                                           self npmjs access token
#                       secrets.PYPI_API_TOKEN                                      self Pypi API token (production site) - https://pypi.org/
#                       secrets.PYPI_API_TEST_TOKEN                                 self Pypi API token (test site) - https://test.pypi.org/
#                       secrets.SELF_DOCKERHUB_TOKEN                                self Dockerhub token
#                       secrets.ORG_BINARYNINJA_TOKEN                               org github personal access token (fine-grained)
#                       secrets.ORG_BINARYNINJA_TOKEN_CL                            org github personal access token (classic)
#                       secrets.ORG_BINARYNINJA_DOCKERHUB_TOKEN                     org dockerhub secret
#                       secrets.ORG_BINARYNINJA_GITEA_TOKEN                         org gitea personal access token (classic) with package:write permission
#                       secrets.BINARYSERV_GPG_KEY_ASC                              bot gpg private key (armored) | BEGIN PGP PRIVATE KEY BLOCK
#                       secrets.BINARYSERV_GPG_PASSPHRASE                           bot gpg private key passphrase
#                       secrets.DISCORD_WEBHOOK_CHAN_GITHUB_TVAPP2_RELEASES         discord webhook to report release notifications from github to discord
#                       secrets.DISCORD_WEBHOOK_CHAN_GITHUB_TVAPP2_WORKFLOWS        discord webhook to report workflow notifications from github to discord
#                       secrets.DISCORD_WEBHOOK_CHAN_GITHUB_TVAPP2_UPDATES          discord webhook to report activity notifications from github to discord
#
#   @local              these workflows can be tested locally through the use of `act`
#                           https://github.com/nektos/act
#                       Extract act to folder
#                       Add system env var with path to act.exe
#                       Run the commands:
#                           git pull https://github.com/username/repo
#                           act -W .github/workflows/documentation.yml -P ubuntu-latest=catthehacker/ubuntu:full-22.04
#                           act -W .github/workflows/documentation.yml -s TOKEN_CL=XXXXXXXXXX --pull=false
# #

name: "📒 Docs › Build"
run-name: "📒 Docs › Build"

# #
#   triggers
# #

on:

    # #
    #   Trigger › Release
    #
    #   update documentation every time a release is made
    # #

    release:
        types:
            - published

    # #
    #   Trigger › Push
    #
    #   update documentation every time a file in docs/ folder is modified
    # #

    push:
        paths:
            - docs/**

    # #
    #   Trigger › Workflow Dispatch
    #
    #   If any values are not provided, will use fallback env variable
    # #

    workflow_dispatch:
        inputs:

            # #
            #   Image Name
            #
            #   used in github image path
            #       ghcr.io/${{ env.IMAGE_GHCR_AUTHOR }}/${{ env.IMAGE_NAME }}
            # #

            WORKING_DIR:
                description:    '📁 Docs Folder'
                required:       true
                default:        './docs/site'
                type:           string

    # #
    #   Trigger › Cron
    #
    #   update documentation every X hours
    # #

    schedule:
        - cron: "0 */12 * * *"

# #
#   environment variables
# #

env:
    WORKING_DIR:                ${{ github.event.inputs.WORKING_DIR || './docs/site' }}
    ASSIGN_USER:                Aetherinox
    BOT_NAME_1:                 EuropaServ
    BOT_NAME_2:                 BinaryServ
    BOT_NAME_DEPENDABOT:        dependabot[bot]
    BOT_NAME_RENOVATE:          renovate[bot]

# #
#   jobs
# #

jobs:
    build-docs:
      # runs-on: ubuntu-latest
        runs-on: apollo-x64
        timeout-minutes: 20
        permissions:
            contents: write
            pages: write
        environment:
            name: Orion
        steps:

            # #
            #   Documentation › Checkout › Arm64
            # #

            - name: >-
                ✅ Checkout
              id: task_docs_checkout
              uses: actions/checkout@v4

            # #
            #   Documentation › Set Env Variables
            # #

            - name: >-
                🕛 Get Timestamp
              id: task_docs_set_timestamp
              run: |
                  echo "NOW=$(date +'%m-%d-%Y %H:%M:%S')" >> $GITHUB_ENV
                  echo "NOW_SHORT=$(date +'%m-%d-%Y')" >> $GITHUB_ENV
                  echo "NOW_LONG=$(date +'%m-%d-%Y %H:%M')" >> $GITHUB_ENV
                  echo "NOW_DOCKER_LABEL=$(date +'%Y%m%d')" >> $GITHUB_ENV

            # #
            #   Documentation › Setup Python
            # #

            - name: >-
                🐍 Setup Python
              id: task_docs_python_setup
              uses: actions/setup-python@v5
              with:
                  python-version: 3.x

            # #
            #   Documentation › Build Documentation
            # #

            - name: >-
                📕 Build Documentation
              id: task_docs_build
              run: |
                  export DOCS_NAME=${{ secrets.DOCS_NAME || 'TVApp2' }}
                  export DOCS_SECRET_L1=${{ secrets.DOCS_SECRET_L1 }}
                  export DOCS_SECRET_L2=${{ secrets.DOCS_SECRET_L2 }}
                  export GH_TOKEN=${{ secrets.ADMINSERV_TOKEN }}

                  pip install mkdocs
                  pip install mkdocs-material
                  pip install mike
                  pip install mkdocs-embed-external-markdown
                  pip install mkdocs-git-committers-plugin-2
                  pip install mkdocs-encryptcontent-plugin
                  pip install mkdocs-redirects mkdocs-glightbox pymdown-extensions mkdocs-git-revision-date-localized-plugin mkdocs-git-authors-plugin mkdocs-link-embeds-plugin
                  cd docs
                  mkdocs build
                  dir
              env:
                  GH_TOKEN: ${{ secrets.ADMINSERV_TOKEN }}

            # #
            #   Documentation › Deploy
            # #

            - name: >-
                💽 Deploy
              id: task_docs_deploy
              uses: peaceiris/actions-gh-pages@v4
              with:
                  personal_token: ${{ secrets.ADMINSERV_TOKEN_CL }}
                  publish_dir: "${{ env.WORKING_DIR }}"


            # #
            #   Documentation › Get Weekly Commits
            # #

            - name: >-
                🕛 Get Weekly Commit List
              id: task_docs_set_weekly_commit_list
              run: |
                  echo 'WEEKLY_COMMITS<<EOF' >> $GITHUB_ENV
                  git log --format="[\`%h\`](${{ github.server_url }}/${{ github.repository }}/commit/%H) %s - %an" --since=7.days >> $GITHUB_ENV
                  echo 'EOF' >> $GITHUB_ENV

            # #
            #   Documentation › Notify Github › Success
            # #

            - name: >-
                🔔 Send Discord Webhook Message (Success)
              id: task_docs_notify_discord_success
              uses: tsickert/discord-webhook@v7.0.0
              if: success()
              with:
                  username: 'Io'
                  avatar-url: 'https://i.imgur.com/8BVDkla.jpg'
                  webhook-url: ${{ secrets.DISCORD_WEBHOOK_CHAN_GITHUB_TVAPP2_WORKfLOWS }}
                  embed-title: "⚙️ ${{ github.workflow_ref }}"
                  embed-url: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
                  embed-thumbnail-url: 'https://i.imgur.com/zDIzE8T.jpg'
                  embed-description: |
                      ## 📦 Documentation Deployment᲼${{ job.status == 'success' && '✅' || '❌' }}

                      A **successful** deployment of TVApp2 documentation has been completed. Changes to documentation will appear in the next 5 minutes.

                      - Documentation: https://thebinaryninja.github.io/tvapp2/
                      - Workflow: `${{ github.workflow }} (#${{github.run_number}})`
                      - Runner: `${{ runner.name }}`
                      - Triggered By: `${{ github.actor }}`
                      - Status: `${{ job.status == 'success' && '✅ Successful' || '❌ Failed' }}`

                  embed-color: ${{ job.status == 'success' && '5763719' || '15418782' }}
                  embed-footer-text: "Completed at ${{ env.NOW }} UTC"
                  embed-timestamp: "${{ env.NOW_LONG }}"
                  embed-author-name: "${{ github.repository_owner }}"
                  embed-author-url: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
                  embed-author-icon-url: "https://avatars.githubusercontent.com/u/200161462"

            # #
            #   Documentation › Notify Github › Failure
            # #

            - name: >-
                🔔 Send Discord Webhook Message (Failure)
              id: task_docs_notify_discord_failure
              uses: tsickert/discord-webhook@v7.0.0
              if: failure()
              with:
                  username: 'Io'
                  avatar-url: 'https://i.imgur.com/8BVDkla.jpg'
                  webhook-url: ${{ secrets.DISCORD_WEBHOOK_CHAN_GITHUB_TVAPP2_WORKfLOWS }}
                  embed-title: "⚙️ ${{ github.workflow_ref }}"
                  embed-url: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
                  embed-thumbnail-url: 'https://i.imgur.com/zDIzE8T.jpg'
                  embed-description: |
                      ## 📦 Documentation Deployment᲼${{ job.status == 'success' && '✅' || '❌' }}

                      A **failed** attempt to build the new documentation has triggered. No changes to your documentation will be made until the errors are fixed in the workflow.

                      - Documentation: https://thebinaryninja.github.io/tvapp2/
                      - Workflow: `${{ github.workflow }} (#${{github.run_number}})`
                      - Runner: `${{ runner.name }}`
                      - Triggered By: `${{ github.actor }}`
                      - Status: `${{ job.status == 'success' && '✅ Successful' || '❌ Failed' }}`

                  embed-color: ${{ job.status == 'success' && '5763719' || '15418782' }}
                  embed-footer-text: "Completed at ${{ env.NOW }} UTC"
                  embed-timestamp: "${{ env.NOW_LONG }}"
                  embed-author-name: "${{ github.repository_owner }}"
                  embed-author-url: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
                  embed-author-icon-url: "https://avatars.githubusercontent.com/u/200161462"

