# -*- coding: utf-8 -*-
path = 'c:/NeoZQYY/docs/h5_ui/compare/AGENT-PLAYBOOK.md'
with open(path, 'rb') as f:
    content = f.read().decode('utf-8')

changes = 0

# 1. Fix 镶点 -> 锚点 (\u9557 -> \u9576)
old_count = content.count('\u9557\u70b9')
content = content.replace('\u9557\u70b9', '\u9576\u70b9')
print(f'Fix \u9576\u70b9: {old_count} replacements')
changes += old_count

# 2. Replace the §二 screenshot params table
# Find the section by unique substring
marker_start = '## \u4e8c\u3001\u622a\u56fe\u53c2\u6570'
marker_end = '## \u4e09\u3001\u622a\u56fe\u64cd\u4f5c'
idx_start = content.find(marker_start)
idx_end = content.find(marker_end)
if idx_start >= 0 and idx_end > idx_start:
    old_section = content[idx_start:idx_end]
    new_section = '''## \u4e8c\u3001\u622a\u56fe\u53c2\u6570\uff08\u53cc\u7aef\u5bf9\u9f50\u57fa\u51c6\uff09

### 2.1 \u5173\u952e\u7ed3\u8bba\uff1aMP \u622a\u56fe\u4e0d\u542b\u72b6\u6001\u680f/\u6807\u9898\u680f/tabBar

`mcp_weixin_devtools_mcp_screenshot` \u622a\u53d6\u7684\u662f\u6a21\u62df\u5668**\u9875\u9762\u5185\u5bb9\u533a\u57df**\uff08WXML \u6e32\u67d3\u5185\u5bb9\uff09\uff0c\u4e0d\u542b\uff1a
- \u72b6\u6001\u680f\uff08statusBar\uff09
- \u7cfb\u7edf \u6807\u9898\u680f\uff08navigationBar\uff09
- \u539f\u751f tabBar\uff08\u5e95\u90e8\u5207\u6362\u680f\uff09

\u540c\u6837\uff0cH5 \u622a\u56fe\u4e5f\u5df2\u9690\u85cf `#bottomNav`\uff08H5 \u6a21\u62df tabBar\uff09\u548c `.safe-area-top`\uff08\u6a21\u62df\u72b6\u6001\u680f\u5360\u4f4d\uff09\u3002
**\u53cc\u7aef\u5bf9\u6bd4\u7684\u5747\u662f\u9875\u9762\u4e3b\u4f53\u5185\u5bb9\uff0c\u5bf9\u9f50\u57fa\u51c6\u4e00\u81f4\u3002**

### 2.2 \u5c3a\u5bf8\u53c2\u6570\u8868

| \u53c2\u6570 | H5 | MP | \u8bf4\u660e |
|------|-----|-----|------|
| viewport \u5bbd | 430px | 430px | H5 `width=430`\uff1bMP \u6a21\u62df\u5668\u9009\u5bbd430px \u673a\u578b |
| viewport \u9ad8 | **752px** | **834px**\uff08windowHeight\uff09 | \u4e0d\u4e00\u81f4\uff01MP \u622a\u56fe\u540e\u5fc5\u987b\u88c1\u526a |
| DPR | 1.5 | 1.5 | H5 `deviceScaleFactor:1.5`\uff1bMP \u539f\u751f DPR=1.5 |
| \u539f\u59cb\u622a\u56fe\u5c3a\u5bf8 | 645\u00d71128 | **645\u00d71251** | \u9ad8\u5ea6\u4e0d\u4e00\u81f4 |
| **\u5bf9\u6bd4\u57fa\u51c6\u5c3a\u5bf8** | **645\u00d71128** | **645\u00d71128**\uff08\u88c1\u526a\u540e\uff09 | \u50cf\u7d20\u5bf9\u6bd4\u7684\u5b9e\u9645\u5c3a\u5bf8 |
| \u6eda\u52a8\u6b65\u957f | 600px | 600px | \u56fa\u5b9a\u6b65\u957f\uff08\u903b\u8f91\u50cf\u7d20\uff09 |
| \u622a\u56fe\u683c\u5f0f | PNG | PNG | `scale:\'device\'` \u8f93\u51fa\u7269\u7406\u50cf\u7d20 |

### 2.3 MP \u622a\u56fe\u540e\u88c1\u526a\uff08\u5fc5\u987b\u6267\u884c\uff09

\u6bcf\u5f20 MP \u622a\u56fe\u540e\u7acb\u5373\u88c1\u526a\uff0c\u4f7f\u7528\u4ee5\u4e0b \u811a\u672c \uff08\u5df2\u5185\u7f6e\u4e8e image_compare MCP \u524d\u7f6e\u6b65\u9aa4\uff09\uff1a

```python
from PIL import Image
img = Image.open(\'mp--step-N.png\')  # 645\u00d71251
img.crop((0, 0, 645, 1128)).save(\'mp--step-N.png\')  # \u88c1\u526a\u540e 645\u00d71128
```

\u6216\u5728 MCP \u622a\u56fe\u8c03\u7528\u540e\u7acb\u5373\u8c03\u7528\uff1a
```
\u2192 mcp_image_compare_crop_image (\u82e5\u53ef\u7528)
  \u6216\u76f4\u63a5\u8c03\u7528 anchor_compare.py crop \u5b50\u547d\u4ee4
```

'''
    content = content[:idx_start] + new_section + content[idx_end:]
    print('Patch: \u00a7\u4e8c section replaced')
    changes += 1
else:
    print(f'ERROR: section markers not found. start={idx_start} end={idx_end}')

# 3. Add MP crop step after screenshot in §3.2 step 3d
# Find the step 3d screenshot call and add crop after
old_3d = "  3d. \u622a\u56fe\r\n    \u2192 mcp_weixin_devtools_mcp_screenshot\r\n      path: \"docs/h5_ui/compare/<page>/mp--step-<scrollTop>.png\""
if old_3d not in content:
    old_3d = old_3d.replace('\r\n', '\n')

if old_3d in content:
    new_3d = old_3d + '''

  3e. \u88c1\u526a MP \u622a\u56fe\u5230\u5bf9\u6bd4\u9ad8\u5ea6\uff08\u5fc5\u987b\u6267\u884c\uff09
    MP \u622a\u56fe\u9ad8\u5ea6\u4e3a 1251px\uff08\u903b\u8f91 834px\uff09\uff0c\u9700\u88c1\u526a\u81f3 1128px\uff08\u903b\u8f91 752px\uff09\u4e0e H5 \u5bf9\u9f50:
    \u2192 mcp_image_compare_crop_image
      image_path: "docs/h5_ui/compare/<page>/mp--step-<scrollTop>.png"
      x: 0
      y: 0
      width: 645
      height: 1128
      output_path: "docs/h5_ui/compare/<page>/mp--step-<scrollTop>.png"
    (\u82e5 mcp \u4e0d\u652f\u6301 crop\uff0c\u6539\u7528 anchor_compare.py\uff1a)
    uv run python scripts/ops/anchor_compare.py crop docs/h5_ui/compare/<page>/mp--step-<scrollTop>.png 0 0 645 1128
'''
    content = content.replace(old_3d, new_3d)
    print('Patch: step 3e crop added')
    changes += 1
else:
    print('Patch 3e: old_3d not found (CRLF issue), trying \\n only')
    old_3d2 = "  3d. \u622a\u56fe\n    \u2192 mcp_weixin_devtools_mcp_screenshot\n      path: \"docs/h5_ui/compare/<page>/mp--step-<scrollTop>.png\""
    if old_3d2 in content:
        new_3d2 = old_3d2 + '\n\n  3e. \u88c1\u526a MP \u622a\u56fe\u5230\u5bf9\u6bd4\u9ad8\u5ea6\uff08\u5fc5\u987b\u6267\u884c\uff09\n    MP \u622a\u56fe\u9ad8\u5ea6\u4e3a 1251px\uff08\u903b\u8f91 834px\uff09\uff0c\u9700\u88c1\u526a\u81f3 1128px\uff08\u903b\u8f91 752px\uff09\u4e0e H5 \u5bf9\u9f50\uff1a\n    uv run python scripts/ops/anchor_compare.py crop <mp_path> 0 0 645 1128\n'
        content = content.replace(old_3d2, new_3d2)
        print('Patch 3e (lf): crop step added')
        changes += 1
    else:
        print('3e not found either, skipping')
        idx = content.find('3d.')
        if idx >= 0:
            print(repr(content[idx:idx+200]))

with open(path, 'wb') as f:
    f.write(content.encode('utf-8'))
print(f'Total changes: {changes}. Done.')
