import re

with open('resources/views/frontend/form.blade.php', 'r') as f:
    html = f.read()

# We need to target Step 2, 3, 4, 5.
# Let's just find all <input type="text"> and <select> without name attributes.
def add_name_to_tags(html):
    # This is a bit risky. Let's do a targeted replace for the family table.
    
    # Table 1: Susunan Keluarga (Istri/Suami dan anak-anak)
    # Suami/Istri, Anak 1, Anak 2, Anak 3
    family_members = ['suami_istri', 'anak_1', 'anak_2', 'anak_3']
    for member in family_members:
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="family_data[core][{member}][name]"></td>', 1)
        html = html.replace('<td><select><option></option><option>L</option><option>P</option></select></td>', f'<td><select name="family_data[core][{member}][gender]"><option></option><option>L</option><option>P</option></select></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="family_data[core][{member}][birth_info]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="family_data[core][{member}][education]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="family_data[core][{member}][occupation]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="family_data[core][{member}][phone]"></td>', 1)

    # Table 2: Susunan Keluarga (Ayah, Ibu, Anak 1, Anak 2, Anak 3)
    family_members_2 = ['ayah', 'ibu', 'saudara_1', 'saudara_2', 'saudara_3']
    for member in family_members_2:
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="family_data[parents][{member}][name]"></td>', 1)
        html = html.replace('<td><select><option></option><option>L</option><option>P</option></select></td>', f'<td><select name="family_data[parents][{member}][gender]"><option></option><option>L</option><option>P</option></select></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="family_data[parents][{member}][birth_info]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="family_data[parents][{member}][education]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="family_data[parents][{member}][occupation]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="family_data[parents][{member}][phone]"></td>', 1)

    # Step 3: Riwayat Pendidikan
    # Pendidikan Formal: SMA, D3, S1, S2
    edu_levels = ['sma', 'd3', 's1', 's2']
    for level in edu_levels:
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="education_data[formal][{level}][school]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="education_data[formal][{level}][major]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="education_data[formal][{level}][location]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="education_data[formal][{level}][year_start]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="education_data[formal][{level}][year_end]"></td>', 1)
        html = html.replace('<td><select><option></option><option>Lulus</option><option>Tidak</option></select></td>', f'<td><select name="education_data[formal][{level}][status]"><option></option><option>Lulus</option><option>Tidak</option></select></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="education_data[formal][{level}][gpa]"></td>', 1)

    # Pendidikan Non Formal: 3 rows
    for i in range(3):
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="education_data[non_formal][{i}][course]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="education_data[non_formal][{i}][organizer]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="education_data[non_formal][{i}][location]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="education_data[non_formal][{i}][year_start]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="education_data[non_formal][{i}][year_end]"></td>', 1)

    # Step 4: Riwayat Pekerjaan
    # Has 2 blocks of textarea and inputs
    for i in range(2):
        html = html.replace('<textarea style="width: 100%; height: 100px; border: none; padding: 10px; resize: none; font-family: inherit; font-size: 0.9rem;" placeholder="Isi di sini..."></textarea>', f'<textarea name="experience_data[{i}][company_info]" style="width: 100%; height: 100px; border: none; padding: 10px; resize: none; font-family: inherit; font-size: 0.9rem;" placeholder="Isi di sini..."></textarea>', 1)
        html = html.replace('<textarea style="width: 100%; height: 100px; border: none; padding: 10px; resize: none; font-family: inherit; font-size: 0.9rem;"></textarea>', f'<textarea name="experience_data[{i}][company_info]" style="width: 100%; height: 100px; border: none; padding: 10px; resize: none; font-family: inherit; font-size: 0.9rem;"></textarea>', 1)
        
        # 12 inputs per block (3 for Jabatan, 3 for Periode, 3 for Gaji, 3 for Bawahan)
        for field in ['jabatan', 'periode', 'gaji', 'bawahan']:
            html = html.replace('<input type="text" style="border: none; border-bottom: 1px solid #eee; padding: 10px; font-size: 0.9rem; flex: 1;">', f'<input type="text" name="experience_data[{i}][{field}][awal]" style="border: none; border-bottom: 1px solid #eee; padding: 10px; font-size: 0.9rem; flex: 1;">', 1)
            html = html.replace('<input type="text" style="border: none; border-bottom: 1px solid #eee; padding: 10px; font-size: 0.9rem; flex: 1;">', f'<input type="text" name="experience_data[{i}][{field}][akhir]" style="border: none; border-bottom: 1px solid #eee; padding: 10px; font-size: 0.9rem; flex: 1;">', 1)
            html = html.replace('<input type="text" style="border: none; padding: 10px; font-size: 0.9rem; flex: 1;">', f'<input type="text" name="experience_data[{i}][{field}][ket]" style="border: none; padding: 10px; font-size: 0.9rem; flex: 1;">', 1)
        
        # Alasan berhenti
        html = html.replace('<input type="text" style="width: 100%; border: 1px solid #eee; padding: 8px; margin-top: 5px; font-size: 0.9rem;">', f'<input type="text" name="experience_data[{i}][alasan_berhenti]" style="width: 100%; border: 1px solid #eee; padding: 8px; margin-top: 5px; font-size: 0.9rem;">', 1)
        # Uraian Tugas
        html = html.replace('<textarea style="width: 100%; height: 60px; border: 1px solid #eee; padding: 8px; margin-top: 5px; resize: none; font-family: inherit; font-size: 0.9rem;"></textarea>', f'<textarea name="experience_data[{i}][uraian_tugas]" style="width: 100%; height: 60px; border: 1px solid #eee; padding: 8px; margin-top: 5px; resize: none; font-family: inherit; font-size: 0.9rem;"></textarea>', 1)

    # Step 5: Lain-lain
    # Aktivitas Sosial 3 rows
    for i in range(3):
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="other_data[aktivitas][{i}][organisasi]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="other_data[aktivitas][{i}][kegiatan]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="other_data[aktivitas][{i}][jabatan]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="other_data[aktivitas][{i}][tahun]"></td>', 1)

    # Hobby
    html = html.replace('<textarea style="width: 100%; border: 1px solid #ccc; padding: 10px; border-radius: 4px; resize: none; font-family: inherit; font-size: 0.95rem; height: 60px;"></textarea>', '<textarea name="other_data[hobby]" style="width: 100%; border: 1px solid #ccc; padding: 10px; border-radius: 4px; resize: none; font-family: inherit; font-size: 0.95rem; height: 60px;"></textarea>', 1)

    # Referensi 3 rows
    for i in range(3):
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="other_data[referensi][{i}][nama]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="other_data[referensi][{i}][alamat]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="other_data[referensi][{i}][jabatan]"></td>', 1)
        html = html.replace('<td><input type="text"></td>', f'<td><input type="text" name="other_data[referensi][{i}][hubungan]"></td>', 1)
        
    # Darurat 4 rows
    for k in ['nama', 'alamat', 'telepon', 'hubungan']:
        html = html.replace('<td style="border: 1px solid #999;"><input type="text"></td>', f'<td style="border: 1px solid #999;"><input type="text" name="other_data[darurat][{k}][1]"></td>', 1)
        html = html.replace('<td style="border: 1px solid #999;"><input type="text"></td>', f'<td style="border: 1px solid #999;"><input type="text" name="other_data[darurat][{k}][2]"></td>', 1)

    # Pertanyaan (q1, q2, q3, q4)
    # the radio buttons already have name="q1", we just need to add names to the keterangan field
    for i in range(1, 5):
        html = html.replace('<td style="border: 1px solid #999;"><input type="text"></td>', f'<td style="border: 1px solid #999;"><input type="text" name="other_data[q{i}_keterangan]"></td>', 1)

    # The radio buttons name need to be array other_data[qX]
    for i in range(1, 5):
        html = html.replace(f'name="q{i}"', f'name="other_data[q{i}]"')

    # Step 6 Approval date and name
    html = html.replace('<input type="text" placeholder="Contoh: Jakarta, 20 Mei 2026" required style="max-width: 300px;">', '<input type="text" name="other_data[signature_date]" placeholder="Contoh: Jakarta, 20 Mei 2026" required style="max-width: 300px;">', 1)
    html = html.replace('<input type="text" required style="max-width: 300px;">', '<input type="text" name="other_data[signature_name]" required style="max-width: 300px;">', 1)

    return html

new_html = add_name_to_tags(html)

with open('resources/views/frontend/form.blade.php', 'w') as f:
    f.write(new_html)

print("Replacement done.")
