# This workflow builds and deploys the Dokka documentation to GitHub Pages.

name: Deploy Documentation

on:
  push:
    branches:
      - main
    paths:
      # Only rebuild docs when source code changes (Dokka generates from KDoc)
      - 'app/src/**'
      - 'core/**/src/**'
      - 'feature/**/src/**'
      - 'desktop/src/**'
      - 'build-logic/**'
      - 'build.gradle.kts'
      - 'settings.gradle.kts'
      - '.github/workflows/docs.yml'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
    inputs:
      ref:
        description: 'The branch, tag or SHA to checkout'
        required: false
        type: string
  
  # Allow this workflow to be called from other workflows
  workflow_call:
    inputs:
      ref:
        description: 'The branch, tag or SHA to checkout'
        required: false
        type: string

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow only one concurrent deployment; cancel queued runs since only the latest
# main state matters for documentation.
concurrency:
  group: "pages"
  cancel-in-progress: true

jobs:
  build-docs:
    if: github.repository == 'meshtastic/Meshtastic-Android'
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          submodules: 'recursive'
          ref: ${{ inputs.ref || '' }}

      - name: Gradle Setup
        uses: ./.github/actions/gradle-setup
        with:
          gradle_encryption_key: ${{ secrets.GRADLE_ENCRYPTION_KEY }}

      - name: Build Dokka HTML documentation
        run: ./gradlew dokkaGeneratePublicationHtml

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v5
        with:
          path: build/dokka/html

  deploy:
    if: github.repository == 'meshtastic/Meshtastic-Android'
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-24.04-arm
    needs: build-docs
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v5
