import re

# --- 1. Fix admin_keuangan/tagihan.html ---
with open('templates/admin_keuangan/tagihan.html', 'r', encoding='utf-8') as f:
    tagihan = f.read()

# Fix active class
tagihan = tagihan.replace('<a href="/admin_keuangan/tagihan"><i', '<a href="/admin_keuangan/tagihan" class="active"><i')
# Ensure no other active classes in sidebar
tagihan = tagihan.replace('class="active"><i class="fas fa-users', '><i class="fas fa-users')

# Clean up duplicate headers
# Remove the inner container fluid header
header_to_remove = """<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="mb-0">Data Tagihan Santri</h2>"""

if header_to_remove in tagihan:
    tagihan = tagihan.replace(header_to_remove, '<div class="container-fluid pt-3">')
    # Move the button to the top header
    btn_add = """<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#tambahTagihanModal">
            <i class="fas fa-plus"></i> Tambah Tagihan Baru
        </button>"""
    tagihan = tagihan.replace(btn_add, '')
    
    # Insert btn_add into the top header (next to the back button or calendar)
    top_header = """<h3 class="text-dark fw-bold mb-2 m-0">Kelola Tagihan Santri</h3>"""
    tagihan = tagihan.replace(top_header, f'<div class="d-flex justify-content-between align-items-center w-100"><div>{top_header}</div><div>{btn_add}</div></div>')

with open('templates/admin_keuangan/tagihan.html', 'w', encoding='utf-8') as f:
    f.write(tagihan)


# --- 2. Fix admin_database/kitab.html ---
with open('templates/admin_database/kitab.html', 'r', encoding='utf-8') as f:
    kitab = f.read()

if '{% block page_header %}' not in kitab:
    kitab = kitab.replace('{% block content %}', '''{% block page_header %}
<h5 class="mb-1 fw-bold" style="color: #1f2937; font-size: 1.25rem;">Data Kitab & Wirid</h5>
<small class="text-muted" style="font-size: 0.85rem;">Kelola seluruh buku wirid yang dapat dibaca oleh santri/wali.</small>
{% endblock %}

{% block content %}''')
    
    # Remove old inner header
    kitab = re.sub(r'<div class="d-flex justify-content-between align-items-center mb-4">\s*<h2 class="mb-0">Data Kitab & Wirid</h2>\s*<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#tambahKitabModal">\s*<i class="fas fa-plus"></i> Tambah Kitab Baru\s*</button>\s*</div>', 
    '''<div class="mt-4 mb-4 d-flex justify-content-end">
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#tambahKitabModal">
            <i class="fas fa-plus"></i> Tambah Kitab Baru
        </button>
    </div>''', kitab)

with open('templates/admin_database/kitab.html', 'w', encoding='utf-8') as f:
    f.write(kitab)


# --- 3. Fix admin_database/kitab_halaman.html ---
with open('templates/admin_database/kitab_halaman.html', 'r', encoding='utf-8') as f:
    halaman = f.read()

if '{% block page_header %}' not in halaman:
    halaman = halaman.replace('{% block content %}', '''{% block page_header %}
<h5 class="mb-1 fw-bold" style="color: #1f2937; font-size: 1.25rem;">Kelola Halaman Kitab</h5>
<small class="text-muted" style="font-size: 0.85rem;">Kitab: {{ kitab.judul_kitab }}</small>
{% endblock %}

{% block content %}''')
    
    # Remove old inner header
    halaman = re.sub(r'<div class="d-flex justify-content-between align-items-center mb-4">\s*<div>\s*<h2 class="mb-0">Kelola Halaman</h2>\s*<p class="text-muted mb-0">Kitab: \{\{ kitab.judul_kitab \}\}</p>\s*</div>\s*<div>\s*<a href="/admin_database/kitab" class="btn btn-secondary me-2"><i class="fas fa-arrow-left"></i> Kembali</a>\s*<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#tambahHalamanModal">\s*<i class="fas fa-plus"></i> Tambah Halaman\s*</button>\s*</div>\s*</div>', 
    '''<div class="mt-4 mb-4 d-flex justify-content-end gap-2">
        <a href="/admin_database/kitab" class="btn btn-secondary"><i class="fas fa-arrow-left"></i> Kembali</a>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#tambahHalamanModal">
            <i class="fas fa-plus"></i> Tambah Halaman
        </button>
    </div>''', halaman)

with open('templates/admin_database/kitab_halaman.html', 'w', encoding='utf-8') as f:
    f.write(halaman)

print("UI Fixed.")
