#!/usr/bin/env python3
"""Apply answer distribution fixes to rewrite_p4_all.py"""
import sys

with open('/root/.openclaw/workspace-xiaoyan/business_production/单元挑战/scripts/rewrite_p4_all.py', 'r') as f:
    content = f.read()

fixes = [
    # 021301_first Q2: answer 0→2
    ('"What does Ben say about the actor?"',
     '["He does his own stunts", "He looks different at home", "He has a fake beard"]',
     0,
     '["He has a fake beard", "He looks different at home", "He does his own stunts"]',
     2,
     'Ben说\'He does his own stunts. He is a real professional.\'，说明这位演员自己做特技动作。选项C为正确答案。'),
    
    # 021301_first Q4: answer 1→2
    ('"What does Lucy say about her neighbour?"',
     '["He is an actor with a fake beard", "He was brave like a hero", "He is kind and helpful"]',
     1,
     '["He is kind and helpful", "He looks different at home", "He was brave like a hero"]',
     2,
     'Lucy说\'My neighbour did something brave last week. He was like a hero.\'，她的邻居做了勇敢的事像英雄一样。选项C为正确答案。'),
    
    # 021401_first Q4: answer 1→2
    ('"What does Ben plan to do this weekend?"',
     '["Read the newspaper at home", "Fix the garden seat", "Buy a new backpack"]',
     1,
     '["Buy a new backpack for hiking", "Read the newspaper at home", "Fix the garden seat"]',
     2,
     'Ben说\'I\'m going to repair the garden seat.\'，他打算修理花园椅子。选项C为正确答案。'),
    
    # 021401_second Q1: answer 2→0
    ('"Whose plan was to prepare for next week\'s trip?"',
     '["Lucy\'s plan", "Skylar\'s plan", "Ben\'s plan"]',
     2,
     '["Ben\'s plan to prepare for next week\'s trip", "Skylar\'s plan to fix the backpack", "Lucy\'s plan to read in the park"]',
     0,
     'Ben说\'I had to plan my trip for next week.\'，他在为下周旅行做准备。选项A为正确答案。'),
    
    # 021401_second Q2: answer 2→0
    ('"What does Lucy say she needs to do?"',
     '["Buy a new backpack for school", "Plan her weekend visit", "Repair a broken seat"]',
     2,
     '["Repair a broken seat", "Buy a new backpack for school", "Plan a weekend visit"]',
     0,
     'Lucy说\'I should repair this seat.\'，她需要修椅子。选项A为正确答案。'),
    
    # 021401_second Q4: answer 2→1
    ('"He forgot his backpack with the map"',
     '',  # will be processed differently
     2,
     '',  # will be processed differently
     1,
     ''),
]

# Simpler approach: just do the dry run again and show the updated distributions
print("Fix approach: Let me re-verify with updated script...")

# Actually let me use a different approach - modify key answer indices directly
import re

# Replace specific patterns
substitutions = []

# Find and replace each question block
lines = content.split('\n')
i = 0
qsid = None
block_name = None
q_index = 0

patches_applied = 0

# Manual targeted fixes by finding specific line patterns
# 021301_first Q2
# Change answer from 0 to 2, reorder options
target_audio_021301_01 = False
target_line_count = 0
for i, line in enumerate(lines):
    if '021301-01.mp3' in line:
        target_audio_021301_01 = True
        # Next few lines contain the question, options, answer
        # Find the answer line for this question
        for j in range(i, min(i+15, len(lines))):
            if lines[j].strip().startswith('0,') or lines[j].strip() == '0,':
                # Check if this is inside a make_qs call for 021301-01
                lines[j] = lines[j].replace('0,', '2,')
                patches_applied += 1
                print(f'  Patched 021301_first Q2: answer 0→2 at line {j+1}')
                break
    
    if '021301-03.mp3' in line:
        for j in range(i, min(i+15, len(lines))):
            if lines[j].strip().startswith('1,') or lines[j].strip() == '1,':
                lines[j] = lines[j].replace('1,', '2,')
                patches_applied += 1
                print(f'  Patched 021301_first Q4: answer 1→2 at line {j+1}')
                break

# Write back
with open('/root/.openclaw/workspace-xiaoyan/business_production/单元挑战/scripts/rewrite_p4_all.py', 'w') as f:
    f.write('\n'.join(lines))

print(f'\nApplied {patches_applied} patches')
print('Note: Option text changes need manual handling in the script')
