import re

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

checklist_replacement = """    <?php if ($isChecklist): ?>
        <?php
        $tasks = $definition['tasks'] ?? [];
        ?>
        <div class="checklist-modal-container" data-instance-id="<?= $instanceId ?>">
            <h5>Zadania do wykonania</h5>
            <div class="checklist-container">
                <?php foreach ($tasks as $task):
                    $isChecked = !empty($instanceData[$task['code']]);
                ?>
                    <div class="form-check mb-2" style="font-size: 1.15rem;">
                        <input class="form-check-input task-checkbox-modal" type="checkbox" value="" style="cursor: pointer; border-radius: 0.25em;"
                            data-task-code="<?= htmlspecialchars($task['code']) ?>" <?= $isChecked ? 'checked' : '' ?> id="chk_<?= htmlspecialchars($task['code']) ?>">
                        <label class="form-check-label pt-1" for="chk_<?= htmlspecialchars($task['code']) ?>" title="<?= htmlspecialchars($task['name']) ?>" style="cursor: pointer;">
                            <?= htmlspecialchars($task['name']) ?>
                        </label>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>"""

content = re.sub(
    r"<\?php if \(\$isChecklist\): \?>.*?</div>\s*</div>", 
    lambda m: checklist_replacement, 
    content, 
    flags=re.DOTALL
)

form_replacement = """                    <form id="transition-form">
                        <?php foreach ($currentNode['ui_hints']['form_schema'] as $field): 
                            $fieldName = $field['name'];
                            $currentValue = $instanceData[$fieldName] ?? null;
                        ?>
                            <?php if ($field['type'] === 'checkbox'): ?>
                                <div class="form-check mb-3" style="font-size: 1.15rem;">
                                    <input type="checkbox" id="<?= htmlspecialchars($fieldName) ?>" name="<?= htmlspecialchars($fieldName) ?>" class="form-check-input" value="1" style="cursor: pointer; border-radius: 0.25em;" <?= (!empty($currentValue) || (!isset($instanceData[$fieldName]) && !empty($field['default']))) ? 'checked' : '' ?>/>
                                    <label for="<?= htmlspecialchars($fieldName) ?>" class="form-check-label pt-1" style="cursor: pointer;"><?= htmlspecialchars($field['label']) ?></label>
                                </div>
                            <?php else: ?>
                                <div class="mb-3">
                                    <label for="<?= htmlspecialchars($fieldName) ?>" class="form-label"><?= htmlspecialchars($field['label']) ?></label>
                                    <?php if ($field['type'] === 'textarea'): ?>
                                        <textarea id="<?= htmlspecialchars($fieldName) ?>" name="<?= htmlspecialchars($fieldName) ?>" class="form-control"><?= htmlspecialchars($currentValue ?? '') ?></textarea>
                                    <?php elseif ($field['type'] === 'select'): ?>
                                        <select id="<?= htmlspecialchars($fieldName) ?>" name="<?= htmlspecialchars($fieldName) ?>" class="form-select">
                                            <?php foreach ($field['options'] as $option): ?>
                                                <option value="<?= htmlspecialchars($option['value']) ?>" <?= $currentValue == $option['value'] ? 'selected' : '' ?>><?= htmlspecialchars($option['label']) ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    <?php else: ?>
                                        <input type="<?= htmlspecialchars($field['type']) ?>" id="<?= htmlspecialchars($fieldName) ?>" name="<?= htmlspecialchars($fieldName) ?>" class="form-control" value="<?= htmlspecialchars((string)($currentValue ?? (($field['default'] ?? '') === 'now' ? date('Y-m-d\\TH:i') : ($field['default'] ?? '')))) ?>">
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </form>"""

content = re.sub(
    r"<form id=\"transition-form\">.*?</form>", 
    lambda m: form_replacement, 
    content, 
    flags=re.DOTALL
)

with open('_get_instance_details.php', 'w', encoding='utf-8') as f:
    f.write(content)
print("File _get_instance_details.php patched successfully.")