name: Download Crowdin Translations

on:
  schedule:
    - cron: '0 0 * * 0' # Every Sunday at midnight
  workflow_dispatch:

jobs:
  download-translations:
    name: Download Translations from Crowdin
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          persist-credentials: false

      - name: Download Translations from Crowdin
        uses: crowdin/github-action@v2
        with:
          upload_sources: false
          upload_translations: false
          download_translations: true
          export_only_approved: true
          push_translations: false
          create_pull_request: false
          crowdin_branch_name: main
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
          CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}

      - name: Run Laravel Pint
        uses: aglipanci/laravel-pint-action@8eedec46a1856977c41667f9c66d5562fa3f9c08 # 2.4

      # The Crowdin and Pint steps run in Docker as root, so new translation
      # files land owned by root. Reclaim them so the host-side git operations
      # in create-pull-request can manage the working tree.
      - name: Fix file ownership
        run: sudo chown -R "$(id -u):$(id -g)" "${{ github.workspace }}"

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: 'chore: update translations and format with pint'
          title: 'New Crowdin Translations'
          body: 'New Crowdin translations by [Crowdin GH Action](https://github.com/crowdin/github-action), formatted by Laravel Pint.'
          branch: 'i18n_crowdin_translations'
          base: 'main'