import re

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

pattern = re.compile(r"<\?php foreach \(\$currentNode\[\'ui_hints\'\]\[\'form_schema\'\] as \$field\): \?>(.*?)<\?php endforeach; \?>", re.DOTALL)

replacement = r"""<\?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">
                                    <input type="hidden" name="<?= $fieldName ?>" value="0">
                                    <input type="checkbox" id="<?= $fieldName ?>" name="<?= $fieldName ?>" class="form-check-input" value="1" <?= (!empty($currentValue) || (!isset($currentValue) && !empty($field['default']))) ? 'checked' : '' ?>> 
                                    <label for="<?= $fieldName ?>" class="form-check-label"><?= $field['label'] ?></label>
                                </div>
                            <?php else: ?>
                                <div class="mb-3">
                                    <label for="<?= $fieldName ?>" class="form-label"><?= $field['label'] ?></label>
                                    <?php if ($field['type'] === 'textarea'): ?>
                                        <textarea id="<?= $fieldName ?>" name="<?= $fieldName ?>" class="form-control"><?= htmlspecialchars((string)($currentValue ?? '')) ?></textarea>
                                    <?php elseif ($field['type'] === 'select'): ?>
                                        <select id="<?= $fieldName ?>" name="<?= $fieldName ?>" class="form-select">
                                            <?php foreach ($field['options'] as $option): 
                                                $selected = ($currentValue !== null && (string)$currentValue === (string)$option['value']) ? 'selected' : '';
                                            ?>
                                                <option value="<?= $option['value'] ?>" <?= $selected ?>><?= $option['label'] ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    <?php else: ?>
                                        <?php 
                                            $defaultVal = ($field['default'] ?? '') === 'now' ? date('Y-m-d\\TH:i') : ($field['default'] ?? '');
                                            $valToUse = $currentValue ?? $defaultVal;
                                        ?>
                                        <input type="<?= $field['type'] ?>" id="<?= $fieldName ?>" name="<?= $fieldName ?>" class="form-control" value="<?= htmlspecialchars((string)$valToUse) ?>">
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>
                        <?php endforeach; ?>" 

if pattern.search(content):
    content = pattern.sub(lambda m: replacement, content, count=1)
    with open('_get_instance_details.php', 'w') as f:
        f.write(content)
    print("Patched successfully!")
else:
    print("Pattern not found!")
