name: Crowdin - pull translations (manual)

on:
  workflow_dispatch:
    inputs:
      assignee:
        type: string
        description: 'GitHub username of the Assignee for the pull request'
        required: true
        default: 'Thalarion'
      reviewer:
        type: string
        description: 'GitHub username of the Reviewer for the pull request'
        required: true
        default: 'Thalarion'

permissions:
  contents: write
  pull-requests: write

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

jobs:
  crowdin-pull-and-pr:
    if: github.ref_name == 'main'  # run only when dispatched from main
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          submodules: recursive

      - name: Setup environment
        uses: ./.github/actions/environment

      - name: Split en.json into per-layout files
        run: nix-shell --run "uv run python core/translations/crowdin.py split"

      - name: Download translations from Crowdin
        uses: crowdin/github-action@v2
        with:
          config: core/translations/crowdin.yml
          upload_sources: false
          download_translations: true
          push_sources: false
          push_translations: false
          create_pull_request: false
        env:
          CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_TOKEN }}
          CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}

      - name: Merge translations
        run: nix-shell --run "uv run python core/translations/crowdin.py merge"

      - name: Regenerate translation signatures
        run: nix-shell --run "uv run make -C core translations"

      - name: Generate GitHub App token
        id: trezor-bot-token
        uses: actions/create-github-app-token@v2
        with:
          app-id: ${{ secrets.TREZOR_BOT_APP_ID }}
          private-key: ${{ secrets.TREZOR_BOT_PRIVATE_KEY }}

      - name: Create PR
        uses: peter-evans/create-pull-request@v8
        with:
          token: ${{ steps.trezor-bot-token.outputs.token }}
          commit-message: "chore(translations): sync Crowdin translations"
          title: "Crowdin translations update"
          body: "Automated update of translations pulled from Crowdin."
          branch: ci/crowdin-sync-${{ github.ref_name }}-${{ github.run_id }}
          base: ${{ github.ref_name }}
          labels: translations
          assignees: ${{ github.event.inputs.assignee }}
          reviewers: ${{ github.event.inputs.reviewer }}
