import re

def reformat_table(html_content, table_id):
    # 1. Ganti card shadow mb-4 default adminLTE/SB-admin ke bule-card
    html_content = re.sub(
        r'<div class="card shadow mb-4">\s*<div class="card-header[^>]*>\s*<h6[^>]*>[^<]*</h6>\s*</div>\s*<div class="card-body">',
        '<div class="bule-card rounded-2 shadow-sm border-0 overflow-hidden bg-white mt-4">',
        html_content
    )
    
    # Atur ulang table class
    html_content = re.sub(
        r'<table class="table table-bordered[^"]*"[^>]*>',
        f'<table class="table mb-0 align-middle table-hover text-start" id="{table_id}" style="font-size: 0.875rem;">',
        html_content
    )
    
    # Atur thead
    html_content = html_content.replace('<thead class="table-dark">', '<thead>\n<tr class="bg-white border-bottom border-light-subtle">')
    html_content = html_content.replace('<thead>', '<thead>\n<tr class="bg-white border-bottom border-light-subtle">')
    html_content = re.sub(r'<thead>\s*<tr>', '<thead>\n<tr class="bg-white border-bottom border-light-subtle">', html_content)
    
    # Ganti <th> style
    html_content = re.sub(r'<th>(.*?)</th>', r'<th class="py-3 px-4 text-uppercase fw-semibold text-muted" style="font-size: 0.7rem; letter-spacing: 0.05em;">\1</th>', html_content)
    
    # Atur tbody
    html_content = html_content.replace('<tbody>', '<tbody class="border-top-0">')
    
    # Atur tr di dalam tbody
    html_content = re.sub(r'<tr>\s*(<td>)', r'<tr class="action-row-hover border-bottom border-light-subtle">\n\1', html_content)
    
    # Hapus </div> untuk card-body jika menggunakan bule-card
    # Ini agak tricky, kita hapus </div> penutup card-body. Biasanya </div></div> sebelum modal.
    # Kita akan abaikan karena mungkin butuh parse spesifik.
    return html_content

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

kitab = reformat_table(kitab, 'kitabTable')

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

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

halaman = reformat_table(halaman, 'halamanTable')

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

print("Tables reformatted.")
