import re

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

old_block = """                        <?php foreach ($currentNode['ui_hints']['form_schema'] as $field): ?>
                        <div class="mb-3">
                            <label for="<?= $field['name'] ?>" class="form-label"><?= $field['label'] ?></label>
                            <?php if ($field['type'] === 'textarea'): ?>
                                <textarea id="<?= $field['name'] ?>" name="<?= $field['name'] ?>" class="form-control"></textarea>
                            <?php elseif ($field['type'] === 'select'): ?>
                                <select id="<?= $field['name'] ?>" name="<?= $field['name'] ?>" class="form-select">
                                    <?php foreach ($field['options'] as $option): ?>
                                        <option value="<?= $option['value'] ?>"><?= $option['label'] ?></option>
                                    <?php endforeach; ?>
                                </select>
                            <?php else: ?>
                                <input type="<?= $field['type'] ?>" id="<?= $field['name'] ?>" name="<?= $field['name'] ?>" class="form-control" value="<?= ($field['default'] ?? '') === 'now' ? date('Y-m-d\\TH:i') : '' ?>">
                            <?php endif; ?>
                        </div>
                        <?php endforeach; ?>"""

new_block = """                        <?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 old_block in content:
    content = content.replace(old_block, new_block)
    with open('_get_instance_details.php', 'w') as f:
        f.write(content)
    print("Patched successfully")
else:
    print("Old block not found!")