name: docs-locale-sync

on:
  push:
    branches:
      - dev
    paths:
      - packages/web/src/content/docs/*.mdx

jobs:
  sync-locales:
    if: false
    #if: github.actor != 'opencode-agent[bot]'
    runs-on: blacksmith-4vcpu-ubuntu-2404
    permissions:
      contents: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          persist-credentials: false
          fetch-depth: 0
          ref: ${{ github.ref_name }}

      - name: Setup Bun
        uses: ./.github/actions/setup-bun

      - name: Setup git committer
        id: committer
        uses: ./.github/actions/setup-git-committer
        with:
          opencode-app-id: ${{ vars.OPENCODE_APP_ID }}
          opencode-app-secret: ${{ secrets.OPENCODE_APP_SECRET }}

      - name: Compute changed English docs
        id: changes
        run: |
          FILES=$(git diff --name-only "${{ github.event.before }}" "${{ github.sha }}" -- ':(glob)packages/web/src/content/docs/*.mdx' || true)
          if [ -z "$FILES" ]; then
            echo "has_changes=false" >> "$GITHUB_OUTPUT"
            echo "No English docs changed in push range"
            exit 0
          fi
          echo "has_changes=true" >> "$GITHUB_OUTPUT"
          {
            echo "files<<EOF"
            echo "$FILES"
            echo "EOF"
          } >> "$GITHUB_OUTPUT"

      - name: Install OpenCode
        if: steps.changes.outputs.has_changes == 'true'
        run: curl -fsSL https://opencode.ai/install | bash

      - name: Sync locale docs with OpenCode
        if: steps.changes.outputs.has_changes == 'true'
        env:
          OPENCODE_API_KEY: ${{ secrets.OPENCODE_API_KEY }}
          OPENCODE_CONFIG_CONTENT: |
            {
              "permission": {
                "*": "deny",
                "read": "allow",
                "edit": "allow",
                "glob": "allow",
                "task": "allow"
              }
            }
        run: |
          opencode run --agent docs --model opencode/gpt-5.3-codex <<'EOF'
            Update localized docs to match the latest English docs changes.

            Changed English doc files:
            <changed_english_docs>
            ${{ steps.changes.outputs.files }}
            </changed_english_docs>

            Requirements:
            1. Update all relevant locale docs under packages/web/src/content/docs/<locale>/ so they reflect these English page changes.
            2. You MUST use the Task tool for translation work and launch subagents with subagent_type `translator` (defined in .opencode/agent/translator.md).
            3. Do not translate directly in the primary agent. Use translator subagent output as the source for locale text updates.
            4. Run translator subagent Task calls in parallel whenever file/locale translation work is independent.
            5. Use only the minimum tools needed for this task (read/glob, file edits, and translator Task). Do not use shell, web, search, or GitHub tools for translation work.
            6. Preserve frontmatter keys, internal links, code blocks, and existing locale-specific metadata unless the English change requires an update.
            7. Keep locale docs structure aligned with their corresponding English pages.
            8. Do not modify English source docs in packages/web/src/content/docs/*.mdx.
            9. If no locale updates are needed, make no changes.
          EOF

      - name: Commit and push locale docs updates
        if: steps.changes.outputs.has_changes == 'true'
        run: |
          if [ -z "$(git status --porcelain)" ]; then
            echo "No locale docs changes to commit"
            exit 0
          fi
          git add -A
          git commit -m "docs(i18n): sync locale docs from english changes"
          git pull --rebase --autostash origin "$GITHUB_REF_NAME"
          git push origin HEAD:"$GITHUB_REF_NAME"
