name: Markdown Link Check

on:
  workflow_call:
    inputs:
      soft-fail:
        description: 'Whether to continue on broken links'
        required: false
        type: boolean
        default: false

permissions:
  contents: read

jobs:
  markdown-link-check:
    name: Check Markdown Links
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: '24'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Create logs directory
        shell: pwsh
        run: |
          New-Item -ItemType Directory -Force -Path logs | Out-Null

      - name: Run markdown link check
        id: link-check
        shell: pwsh
        run: |
          & scripts/linting/Markdown-Link-Check.ps1
        continue-on-error: ${{ inputs.soft-fail }}

      - name: Upload markdown link check results
        if: always()
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: markdown-link-check-results
          path: logs/markdown-link-check-results.json
          retention-days: 30

      - name: Check results and fail if needed
        if: ${{ !inputs.soft-fail && steps.link-check.outcome == 'failure' }}
        shell: pwsh
        run: |
          Write-Host "Markdown link check failed and soft-fail is false. Failing the job."
          exit 1
