name: Markdown Lint

on:
  workflow_call:
    inputs:
      soft-fail:
        description: 'Whether to continue on markdown lint violations'
        required: false
        type: boolean
        default: false

# Minimal permissions
permissions:
  contents: read

jobs:
  markdown-lint:
    name: Markdown Lint
    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
        run: mkdir -p logs

      - name: Run markdown lint
        id: markdown-lint
        run: |
          npm run lint:md > markdown-lint-output.txt 2>&1 || echo "MARKDOWN_LINT_FAILED=true" >> "$GITHUB_ENV"
          cat markdown-lint-output.txt
        continue-on-error: true

      - name: Create annotations
        if: env.MARKDOWN_LINT_FAILED == 'true'
        run: |
          echo "::warning::Markdown lint found violations. Review markdown-lint-output.txt artifact for details."

      - name: Upload markdown lint results
        if: always()
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: markdown-lint-results
          path: markdown-lint-output.txt
          retention-days: 30

      - name: Add job summary
        if: always()
        run: |
          if [ "${{ env.MARKDOWN_LINT_FAILED }}" == "true" ]; then
            {
              echo "## Markdown Lint Results"
              echo "❌ **Status**: Failed"
              echo ""
              echo "Markdown linting violations detected. Please review the artifact for details."
            } >> "$GITHUB_STEP_SUMMARY"
          else
            {
              echo "## Markdown Lint Results"
              echo "✅ **Status**: Passed"
              echo ""
              echo "No markdown linting violations detected."
            } >> "$GITHUB_STEP_SUMMARY"
          fi

      - name: Fail job if violations found
        if: env.MARKDOWN_LINT_FAILED == 'true' && !inputs.soft-fail
        run: |
          echo "Markdown lint failed"
          exit 1
