import re

with open('index.php', 'r', encoding='utf-8') as f:
    content = f.read()

# 1. Update Inne Procesy dots
content = re.sub(
    r'<td class="text-center align-middle" data-bs-toggle="modal" data-bs-target="#instanceModal" data-person-id="<\?= \$person[\'id\'] \?>" data-process-id="<\?= \$process[\'id\'] \?>(.*?)">',
    r'<td class="text-center align-middle" data-bs-toggle="modal" data-bs-target="#instanceModal" data-subject-type="person" data-subject-id="<?= $person[\'id\'] ?>" data-process-id="<?= $process[\'id\'] ?>"\1> ',
    content,
    flags=re.DOTALL
)

# 2. Update meeting dots initial HTML
content = re.sub(
    r'data-process-id="<\?= \$mp_def[\'id\'] \?>"\s*data-subject-type="meeting"\s*data-subject-id=""',
    r'data-process-id="<?= $mp_def[\'id\'] ?>" data-process-code="meeting_preparation" data-subject-type="meeting" data-subject-id="" data-cycle-key=""',
    content
)

# 3. Update updateMeetingView JS function
# Find currentMeetingId assignment
content = re.sub(
    r"const currentMeetingId = typeof currentMeetingObj === 'object' \? currentMeetingObj.meeting_id : null;",
    r"const currentMeetingId = typeof currentMeetingObj === 'object' ? currentMeetingObj.meeting_id : null;\n                const currentCycleKey = typeof currentMeetingObj === 'object' ? currentMeetingObj.cycle_key : null;",
    content
)

# Find dot.dataset.subjectId = currentMeetingId;
content = re.sub(
    r'dot.dataset.subjectId = currentMeetingId;',
    r"dot.dataset.subjectId = currentMeetingId;\n                        dot.dataset.cycleKey = currentCycleKey;\n                        dot.dataset.processCode = 'meeting_preparation';\n                        dot.dataset.subjectType = 'meeting';",
    content
)

with open('index.php', 'w', encoding='utf-8') as f:
    f.write(content)
print("index.php patched (step 1-3)")
