name: 'mkdlint'
description: 'Fast Markdown linter with auto-fix and SARIF support'
author: '192d-Wing'

branding:
  icon: 'check-circle'
  color: 'blue'

inputs:
  files:
    description: 'Files or directories to lint (space-separated)'
    required: true
    default: '.'

  version:
    description: 'mkdlint version to use (e.g., "0.8.0" or "latest")'
    required: false
    default: 'latest'

  use-binary:
    description: 'Use pre-built binary (true) or build from source (false)'
    required: false
    default: 'true'

  config:
    description: 'Path to configuration file'
    required: false
    default: ''

  output-format:
    description: 'Output format: text, json, or sarif'
    required: false
    default: 'sarif'

  sarif-file:
    description: 'Path to SARIF output file'
    required: false
    default: 'mkdlint.sarif'

  fix:
    description: 'Automatically fix violations where possible'
    required: false
    default: 'false'

  ignore:
    description: 'Glob patterns to ignore (comma-separated)'
    required: false
    default: ''

  enable:
    description: 'Rules to enable (comma-separated, e.g., "MD001,MD003")'
    required: false
    default: ''

  disable:
    description: 'Rules to disable (comma-separated, e.g., "MD013,MD033")'
    required: false
    default: ''

  no-color:
    description: 'Disable colored output'
    required: false
    default: 'false'

  verbose:
    description: 'Verbose output with detailed information'
    required: false
    default: 'false'

  quiet:
    description: 'Quiet mode - only show file names with errors'
    required: false
    default: 'false'

  fail-on-error:
    description: 'Fail the action if errors are found'
    required: false
    default: 'true'

  upload-sarif:
    description: 'Upload SARIF file to GitHub Code Scanning'
    required: false
    default: 'true'

  working-directory:
    description: 'Working directory for mkdlint'
    required: false
    default: '.'

  changed-only:
    description: 'Only lint changed markdown files in pull requests'
    required: false
    default: 'false'

  job-summary:
    description: 'Write a rich summary to the GitHub Actions job summary'
    required: false
    default: 'true'

outputs:
  exit-code:
    description: 'Exit code from mkdlint'
    value: ${{ steps.run.outputs.exit-code }}

  error-count:
    description: 'Number of errors found'
    value: ${{ steps.run.outputs.error-count }}

  warning-count:
    description: 'Number of warnings found'
    value: ${{ steps.run.outputs.warning-count }}

  file-count:
    description: 'Number of files with issues'
    value: ${{ steps.run.outputs.file-count }}

  duration-ms:
    description: 'Linting duration in milliseconds'
    value: ${{ steps.run.outputs.duration-ms }}

  sarif-file:
    description: 'Path to SARIF output file'
    value: ${{ steps.run.outputs.sarif-file }}

  binary-path:
    description: 'Path to mkdlint binary'
    value: ${{ steps.setup.outputs.binary-path }}

  cache-hit:
    description: 'Whether binary was found in cache'
    value: ${{ steps.cache.outputs.cache-hit }}

runs:
  using: 'composite'
  steps:
    - name: Cache mkdlint binary
      id: cache
      uses: actions/cache@v4
      if: inputs.use-binary == 'true'
      with:
        path: ~/.mkdlint-bin
        key: mkdlint-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}
        restore-keys: |
          mkdlint-${{ runner.os }}-${{ runner.arch }}-

    - name: Setup mkdlint
      id: setup
      shell: bash
      run: |
        "${{ github.action_path }}/action.sh" setup \
          "${{ inputs.version }}" \
          "${{ inputs.use-binary }}" \
          "${{ steps.cache.outputs.cache-hit }}"

    - name: Detect changed files
      id: changed
      if: inputs.changed-only == 'true' && github.event_name == 'pull_request'
      shell: bash
      working-directory: ${{ inputs.working-directory }}
      run: |
        CHANGED=$("${{ github.action_path }}/action.sh" changed-files "${{ github.base_ref }}")
        echo "files=$CHANGED" >> "$GITHUB_OUTPUT"
        if [ -z "$CHANGED" ]; then
          echo "skip=true" >> "$GITHUB_OUTPUT"
        else
          echo "skip=false" >> "$GITHUB_OUTPUT"
        fi

    - name: Run mkdlint
      id: run
      shell: bash
      working-directory: ${{ inputs.working-directory }}
      continue-on-error: true
      if: steps.changed.outputs.skip != 'true'
      run: |
        # Use changed files if incremental mode, otherwise use configured files
        if [ "${{ inputs.changed-only }}" = "true" ] && [ "${{ github.event_name }}" = "pull_request" ] && [ -n "${{ steps.changed.outputs.files }}" ]; then
          LINT_FILES="${{ steps.changed.outputs.files }}"
        else
          LINT_FILES="${{ inputs.files }}"
        fi

        "${{ github.action_path }}/action.sh" run \
          "${{ steps.setup.outputs.binary-path }}" \
          "$LINT_FILES" \
          "${{ inputs.config }}" \
          "${{ inputs.output-format }}" \
          "${{ inputs.sarif-file }}" \
          "${{ inputs.fix }}" \
          "${{ inputs.ignore }}" \
          "${{ inputs.enable }}" \
          "${{ inputs.disable }}" \
          "${{ inputs.no-color }}" \
          "${{ inputs.verbose }}" \
          "${{ inputs.quiet }}"

    - name: Skip summary (no changed files)
      id: run-skip
      if: steps.changed.outputs.skip == 'true'
      shell: bash
      run: |
        echo "exit-code=0" >> "$GITHUB_OUTPUT"
        echo "error-count=0" >> "$GITHUB_OUTPUT"
        echo "warning-count=0" >> "$GITHUB_OUTPUT"
        echo "file-count=0" >> "$GITHUB_OUTPUT"
        echo "duration-ms=0" >> "$GITHUB_OUTPUT"
        echo "sarif-file=" >> "$GITHUB_OUTPUT"
        echo "## :white_check_mark: mkdlint Results" >> "$GITHUB_STEP_SUMMARY"
        echo "" >> "$GITHUB_STEP_SUMMARY"
        echo "> No changed markdown files found in this PR. Skipped linting." >> "$GITHUB_STEP_SUMMARY"

    - name: Write job summary
      if: inputs.job-summary == 'true' && steps.changed.outputs.skip != 'true'
      shell: bash
      working-directory: ${{ inputs.working-directory }}
      run: |
        "${{ github.action_path }}/action.sh" summary \
          "${{ steps.run.outputs.error-count }}" \
          "${{ steps.run.outputs.warning-count }}" \
          "${{ steps.run.outputs.file-count }}" \
          "${{ steps.run.outputs.duration-ms }}" \
          "${{ inputs.fix }}" \
          "${{ steps.run.outputs.exit-code }}" \
          "${{ inputs.sarif-file }}" \
          "${{ inputs.output-format }}"

    - name: Upload SARIF file
      if: inputs.upload-sarif == 'true' && inputs.output-format == 'sarif' && steps.changed.outputs.skip != 'true'
      uses: github/codeql-action/upload-sarif@v3
      with:
        sarif_file: ${{ inputs.working-directory }}/${{ inputs.sarif-file }}
        category: mkdlint
        wait-for-processing: true

    - name: Check results
      shell: bash
      if: inputs.fail-on-error == 'true' && steps.run.outputs.exit-code != '0' && steps.run.outputs.exit-code != ''
      run: |
        echo "::error::mkdlint found errors"
        exit ${{ steps.run.outputs.exit-code }}
