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@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
        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
