
import os

file_path = 'custom_admin/templates/custom_admin/pages/study_abroad/profiles_list.html'

with open(file_path, 'r') as f:
    content = f.readlines()

new_content = []
skip_next = False

for i, line in enumerate(content):
    if skip_next:
        skip_next = False
        continue
    
    # Check for the split Country tag
    if '{{ profile.preferred_location_country|default:"Not' in line:
        # Check if the next line completes it
        if i + 1 < len(content) and 'specified" }}' in content[i+1]:
            print(f"Found split Country tag at line {i+1}. Joining...")
            # specific indentation handling might be needed but let's just make the line clean
            # content[i] has line break at end
            stripped_line = line.rstrip()
            next_line_stripped = content[i+1].strip()
            # Construct the single line
            # Preserve indentation of first line
            joined_line = stripped_line + " " + next_line_stripped + "\n"
            # It should look like: ...|default:"Not specified" }}...
            # The previous line ended with "Not
            # The next line starts with specified" }}
            # So "Not" + " " + "specified" = "Not specified"
            
            # Correcting the string specifically to match desired output
            # We want: ...|default:"Not specified" }}</p>
            # The part in line [i] is: ...|default:"Not
            # The part in line [i+1] is: specified" }}</p>
            
            # Actually, let's just replace the exact known bad string logic
            new_line = line.replace('\n', '') + ' ' + content[i+1].strip() + '\n'
            # But wait, "Not" + " " + "specified" -> "Not specified"
            # "Not" is at end of line?
            # Let's clean it up properly using regex or string replacement on the full text
            new_content.append(new_line) # This is risky if I get it wrong
            # Let's use string replacement on full text instead
            pass
        else:
             new_content.append(line)
    else:
        # Check for Program tag
        if '{{ profile.preferred_study_program|default:"Not' in line:
             if i + 1 < len(content) and 'specified" }}' in content[i+1]:
                 print(f"Found split Program tag at line {i+1}. Joining...")
                 # Skip
                 pass
             else:
                 new_content.append(line)
        else:
             new_content.append(line)

# Let's do full text replacement, it is safer
with open(file_path, 'r') as f:
    full_text = f.read()

# Pattern 1
# Note the spaces/tabs logic. 
# We look for: |default:"Not\n\s+specified"
# We replace with: |default:"Not specified"

import re

fixed_text = re.sub(r'\|default:"Not\s*\n\s*specified"', '|default:"Not specified"', full_text)

if fixed_text != full_text:
    print("Fixed text using regex.")
    with open(file_path, 'w') as f:
        f.write(fixed_text)
else:
    print("No changes made (regex did not match).")
    # Debug
    print("Snippet of file around 'default':")
    start = full_text.find('default:"Not')
    if start != -1:
        print(repr(full_text[start:start+50]))
    else:
        print("Could not find 'default:\"Not' in file.")

