name: Translate

on:
  push:
    branches: [main]

permissions:
  contents: write

jobs:
  translate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Setup pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 9

      - name: Install dependencies
        run: pnpm install --filter @kan/web...

      - name: Extract translations
        working-directory: apps/web
        run: pnpm lingui:extract

      - name: Lingo.dev
        uses: lingodotdev/lingo.dev@main
        with:
          api-key: ${{ secrets.LINGODOTDEV_API_KEY }}
          commit-message: "chore: update translations"
          working-directory: apps/web
          commit-author-name: "${{ github.actor }}"
          commit-author-email: "${{ github.actor }}@users.noreply.github.com"
          parallel: true

      - name: Compile translations
        working-directory: apps/web
        run: pnpm lingui:compile

      - name: Commit compiled translations
        run: |
          git config --local user.email "${{ github.actor }}@users.noreply.github.com"
          git config --local user.name "${{ github.actor }}"
          if ls apps/web/src/locales/*/messages.ts 1> /dev/null 2>&1; then
            git add apps/web/src/locales/*/messages.ts
            git diff --staged --quiet || (git commit -m "chore: compile translations" && git push)
          fi
