name: AI README Review

on:
  workflow_dispatch:
    inputs:
      Input:
        description: 'Input README file'
        required: true
        default: 'README.md'
      Output:
        description: 'Output README file'
        required: true
        default: 'README.md'
      Language:
        description: 'Target language'
        required: true
        default: 'English'
      PR:
        description: 'Creat Pull Request'
        default: false
        type: boolean

jobs:
  ai_review:
    runs-on: ubuntu-latest

    permissions:
      contents: write
      pull-requests: write
      models: read

    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Read README
        id: read_file
        run: |
          README=$(cat ${{ github.event.inputs.Input }})
          {
            echo "readme<<EOF"
            echo "$README"
            echo "EOF"
          } >> "$GITHUB_OUTPUT"

      - name: AI-powered README file review
        id: ai_review
        uses: actions/ai-inference@v2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          endpoint: https://models.github.ai/inference
          model: gpt-4.1
          max-tokens: 8192
          system-prompt: |
            You are a professional translator.
            You must analyze the provided text from the repository's README file and check for spelling in the ${{ github.event.inputs.Language }} language and errors in the text.
            Do not modify the content, you must return the entire text with corrections and without unnecessary comments.
            Preserve the original Markdown formatting and links.
          prompt: ${{ steps.read_file.outputs.readme }}

      - name: Write README
        env:
          README_REVIEW: ${{ steps.ai_review.outputs.response }}
        run: |
          cat <<'EOF' > ${{ github.event.inputs.Output }}
          ${{ env.README_REVIEW }}
          EOF

      - name: Output diff to summary
        run: |
          {
            echo "### 📝 README diff"
            echo '```diff'
            git diff ${{ github.event.inputs.Output }}
            echo '```'
          } >> $GITHUB_STEP_SUMMARY

      - name: Create PR
        if: ${{ github.event.inputs.PR == 'true' }}
        uses: peter-evans/create-pull-request@v8
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: "review readme file using ai"
          branch: "docs/readme"
          title: "README review"
          body: "AI-powered README file review"
          add-paths: |
            ${{ github.event.inputs.Output }}