name: i18n Validate

on:
  pull_request:
  push:
    branches: [main]
  workflow_dispatch:

permissions:
  contents: read

jobs:
  validate:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Detect relevant changes
        id: changes
        env:
          EVENT_NAME: ${{ github.event_name }}
          BASE_SHA: ${{ github.event.pull_request.base.sha }}
          HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
          PUSH_BEFORE: ${{ github.event.before }}
          PUSH_AFTER: ${{ github.event.after }}
        run: |
          set -e
          case "$EVENT_NAME" in
            pull_request)
              git fetch --no-tags --depth=1 origin "$BASE_SHA" 2>/dev/null || true
              range="${BASE_SHA}...${HEAD_SHA}"
              ;;
            push)
              if [ -n "$PUSH_BEFORE" ] && [ "$PUSH_BEFORE" != "0000000000000000000000000000000000000000" ]; then
                range="${PUSH_BEFORE}...${PUSH_AFTER}"
              else
                # initial push or unknown before — assume relevant
                echo "relevant=true" >> "$GITHUB_OUTPUT"
                exit 0
              fi
              ;;
            *)
              echo "relevant=true" >> "$GITHUB_OUTPUT"
              exit 0
              ;;
          esac
          changed=$(git diff --name-only "$range" || true)
          if echo "$changed" | grep -qE '^(skills/|agents/|manifest\.json$|categories\.json$|scripts/i18n/)'; then
            echo "relevant=true" >> "$GITHUB_OUTPUT"
          else
            echo "relevant=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Install uv
        if: steps.changes.outputs.relevant == 'true'
        uses: astral-sh/setup-uv@v3

      - name: Validate i18n
        if: steps.changes.outputs.relevant == 'true'
        run: uv run --quiet scripts/i18n/validate-i18n.py

      - name: Check for stale translations
        if: steps.changes.outputs.relevant == 'true'
        run: uv run --quiet scripts/i18n/translate.py --check
        continue-on-error: true

      - name: Skip notice
        if: steps.changes.outputs.relevant != 'true'
        run: echo "No i18n-relevant changes; validation skipped."
