
import os

file_path = r'c:\xamp\htdocs\back\website\index.php'

with open(file_path, 'r', encoding='utf-8') as f:
    content = f.read()

# Define the target block with loose whitespace matching awareness or just hardcode the known string from view_file
# The view_file output suggested a mix of tabs/spaces.
# I will try to find the specific unique lines and replace the range.

start_marker = '<span><?php echo htmlspecialchars($profile[\'resident_city\']); ?></span>'
end_marker = '<span>Height: 155Cms</span>'

start_idx = content.find(start_marker)
end_idx = content.find(end_marker)

if start_idx != -1 and end_idx != -1:
    # Find the closing </div> tag after end_marker
    div_idx = content.find('</div>', end_idx)
    
    if div_idx != -1:
        # We replace from start_idx to div_idx (exclusive of </div>, but we want to include the div close in our new string or just keep it?)
        # Let's replace the content INSIDE the pro-bio div.
        
        # New Content
        new_block = """<span><?php echo htmlspecialchars($profile['resident_city']); ?></span>
                                            <?php 
                                            $age = 'N/A';
                                            if(!empty($profile['dob'])) {
                                                try {
                                                    $d = new DateTime($profile['dob']);
                                                    $age = $d->diff(new DateTime())->y;
                                                } catch(Exception $e){}
                                            }
                                            ?>
                                            <span><?php echo $age; ?> Years old</span>
                                            <span>Height: <?php echo htmlspecialchars($profile['height']); ?></span>
                                            <span><?php echo htmlspecialchars($profile['occupation']); ?></span>
                                            <span style="font-weight:bold; color:#555;">Caste: <?php echo htmlspecialchars($profile['cast_name']); ?></span>
                                        """
        
        # We need to be careful not to replace too much.
        # The Original was:
        # <span>City</span>
        # <span>96 Kuli ...</span>
        # <span>IT ...</span>
        # <span>29 ...</span>
        # <span>Height ...</span>
        
        # I will replace from start_marker to end_marker + len(end_marker)
        
        original_chunk = content[start_idx:end_idx + len(end_marker)]
        print(f"Found chunk: {original_chunk}")
        
        new_content = content.replace(original_chunk, new_block)
        
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(new_content)
        print("Successfully replaced content.")
    else:
        print("Could not find closing div.")
else:
    print("Could not find start/end markers.")
