name: KDoc → YouTrack KB Sync

permissions:
  contents: read

concurrency:
  group: kdoc-sync-${{ github.ref }}
  cancel-in-progress: true

on:
  workflow_dispatch:
    inputs:
      kb_root_title:
        description: "Root-Titel der YouTrack KB (z. B. 'API & Entwicklerdoku')"
        required: true
        default: "API & Entwicklerdoku"
      bc_root:
        description: "Ordner unterhalb des KB-Roots für BCs (z. B. 'BCs')"
        required: false
        default: "BCs"

jobs:
  kdoc-sync:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Setup JDK 21
        uses: actions/setup-java@v5
        with:
          distribution: temurin
          java-version: '21'

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v5

      - name: Build Dokka (GFM)
        run: |
          ./gradlew --no-daemon dokkaGfmAll || ./gradlew --no-daemon dokkaGfm

      - name: Python deps for YouTrack sync
        run: |
          python -m pip install --upgrade pip
          pip install requests pyyaml

      - name: Sync KDoc Markdown to YouTrack KB
        if: ${{ hashFiles('.junie/scripts/youtrack-sync-kb.py') != '' }}
        env:
          YT_URL: ${{ secrets.YT_URL }}
          YT_TOKEN: ${{ secrets.YT_TOKEN }}
          KB_ROOT_TITLE: ${{ github.event.inputs.kb_root_title }}
          KB_BC_ROOT: ${{ github.event.inputs.bc_root }}
        run: |
          python .junie/scripts/youtrack-sync-kb.py --src build/dokka/gfm
