name: ms.date Freshness Check

on:
  workflow_call:
    inputs:
      staleness-threshold-days:
        description: 'Number of days before a document is considered stale'
        required: false
        type: number
        default: 90
      changed-files-only:
        description: 'Only check files changed in the PR'
        required: false
        type: boolean
        default: false
      soft-fail:
        description: 'Whether to continue when stale files are found'
        required: false
        type: boolean
        default: false

permissions:
  contents: read

jobs:
  msdate-freshness:
    name: Check ms.date Freshness
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
          fetch-depth: 0

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

      - name: Install PowerShell-Yaml
        shell: pwsh
        run: |
          Install-Module -Name PowerShell-Yaml -RequiredVersion 0.4.7 -Force -Scope CurrentUser

      - name: Run ms.date freshness check
        shell: pwsh
        env:
          THRESHOLD_DAYS: ${{ inputs.staleness-threshold-days }}
          CHANGED_FILES_ONLY: ${{ inputs.changed-files-only }}
        run: |
          $params = @{
              ThresholdDays = [int]$env:THRESHOLD_DAYS
          }
          if ($env:CHANGED_FILES_ONLY -eq 'true') {
              $params['ChangedFilesOnly'] = $true
          }
          & scripts/linting/Invoke-MsDateFreshnessCheck.ps1 @params
        continue-on-error: ${{ inputs.soft-fail }}

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

