name: Sync Crowdin

on:
  workflow_dispatch:
    inputs:
      operation:
        description: "Choose which Crowdin sync operation to run"
        required: true
        default: push
        type: choice
        options:
          - push
          - pull

permissions:
  contents: write
  pull-requests: write

concurrency:
  group: crowdin-sync
  cancel-in-progress: true

env:
  PYTHON_VERSION: "3.11"

jobs:
  sync-crowdin:
    name: Sync Crowdin
    runs-on: ubuntu-latest
    env:
      CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
      CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          fetch-depth: 0

      - name: Validate Crowdin secrets
        run: |
          test -n "$CROWDIN_PROJECT_ID" || (echo "Missing CROWDIN_PROJECT_ID secret" && exit 1)
          test -n "$CROWDIN_PERSONAL_TOKEN" || (echo "Missing CROWDIN_PERSONAL_TOKEN secret" && exit 1)

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Install Qt translation tools
        run: |
          python -m pip install --upgrade pip
          pip install --timeout=300 "pyside6~=6.10.3"

      - name: Refresh Qt translation assets
        run: python scripts/sync_i18n_res.py

      - name: Upload sources and translations
        if: github.event.inputs.operation == 'push'
        uses: crowdin/github-action@v2
        with:
          config: crowdin.yml
          upload_sources: true
          upload_translations: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Download translations
        if: github.event.inputs.operation == 'pull'
        uses: crowdin/github-action@v2
        with:
          config: crowdin.yml
          download_translations: true
          push_translations: false
          create_pull_request: false
          skip_ref_checkout: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Prepare downloaded translations
        if: github.event.inputs.operation == 'pull'
        run: |
          sudo chown -R "$(id -u):$(id -g)" .git app/assets
          rm -f .git/COMMIT_EDITMSG
          python scripts/sync_i18n_res.py

      - name: Update Crowdin contributors
        if: github.event.inputs.operation == 'pull'
        uses: andrii-bodnar/action-crowdin-contributors@v3
        with:
          files: |
            README.md
            README_zh.md
          contributors_per_line: 8
          max_contributors: 32
          image_size: 64
          min_words_contributed: 100
        env:
          CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
          CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}

      - name: Create translation pull request
        if: github.event.inputs.operation == 'pull'
        uses: peter-evans/create-pull-request@v8
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          base: ${{ github.ref_name }}
          branch: chore/crowdin-translations
          delete-branch: true
          author: Crowdin Bot <support+bot@crowdin.com>
          committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
          title: "chore(i18n): sync translations from Crowdin"
          commit-message: "chore(i18n): sync translations from Crowdin"
          body: |
            This PR was created automatically by the Crowdin sync workflow.

            It refreshes the Qt `.ts` files, compiled `.qm` files,
            `app/assets/resources.py`, and Crowdin contributors in both READMEs.
          add-paths: |
            app/assets/i18n/*.ts
            app/assets/i18n/*.qm
            app/assets/resources.py
            README.md
            README_zh.md
