import re

with open('resources/views/livewire/worker-data-update/family-relations-form.blade.php', 'r') as f:
    content = f.read()

# Split the content by the section comments
# Section 1: Funcionarios Públicos
# Section 2: Trabajadores de SMCV
# Section 3: Relación Familiar / Empresarial empresas contratistas
# Section 4: Declaración Jurada

sec1_match = re.search(r'(\{\{-- Sección 1:.*?)(?=\{\{-- Sección 2:)', content, re.DOTALL)
sec2_match = re.search(r'(\{\{-- Sección 2:.*?)(?=\{\{-- Sección 3:)', content, re.DOTALL)
sec3_match = re.search(r'(\{\{-- Sección 3:.*?)(?=\{\{-- Sección 4:)', content, re.DOTALL)

if not (sec1_match and sec2_match and sec3_match):
    print("Could not find sections")
    exit(1)

sec1 = sec1_match.group(1)
sec2 = sec2_match.group(1)
sec3 = sec3_match.group(1)

# Modify section titles to reflect new order
sec2 = sec2.replace('Sección 2:', 'Sección 1:')
sec3 = sec3.replace('Sección 3:', 'Sección 2:')
sec1 = sec1.replace('Sección 1:', 'Sección 3:')

# Replace the original sections block with the reordered block
old_block = sec1_match.group(1) + sec2_match.group(1) + sec3_match.group(1)
new_block = sec2 + sec3 + sec1

content = content.replace(old_block, new_block)

with open('resources/views/livewire/worker-data-update/family-relations-form.blade.php', 'w') as f:
    f.write(content)

print("Sections reordered.")
