import sys
import re

def get_balanced_div(html, start_idx):
    i = start_idx
    tag_count = 0
    while i < len(html):
        next_open = html.find('<div', i)
        next_close = html.find('</div>', i)
        
        if next_open == -1 and next_close == -1:
            break
            
        if next_open != -1 and (next_open < next_close or next_close == -1):
            tag_count += 1
            i = next_open + 4
        else:
            tag_count -= 1
            i = next_close + 6
            if tag_count == 0:
                return start_idx, i
    return start_idx, -1

def get_balanced_ul(html, start_idx):
    i = start_idx
    tag_count = 0
    while i < len(html):
        next_open = html.find('<ul', i)
        next_close = html.find('</ul>', i)
        
        if next_open == -1 and next_close == -1:
            break
            
        if next_open != -1 and (next_open < next_close or next_close == -1):
            tag_count += 1
            i = next_open + 3
        else:
            tag_count -= 1
            i = next_close + 5
            if tag_count == 0:
                return start_idx, i
    return start_idx, -1

html = open('app/modules/sag/templates/detail.html.bak').read()

def extract_widget(html, data_module_name):
    matches = list(re.finditer(rf'<div[^>]*data-module="{data_module_name}"[^>]*>', html))
    if not matches: return "", html
    start, end = get_balanced_div(html, matches[0].start())
    widget = html[start:end]
    html = html[:start] + html[end:]
    return widget, html

def extract_by_comment(html, comment_str):
    c_start = html.find(comment_str)
    if c_start == -1: return "", html
    div_start = html.find('<div', c_start)
    if div_start == -1: return "", html
    start, end = get_balanced_div(html, div_start)
    widget = html[c_start:end]
    html = html[:c_start] + html[end:]
    return widget, html

def extract_ul_nav(html):
    start = html.find('<ul class="nav nav-tabs')
    if start == -1: return "", html
    c_start = html.rfind('<!-- Tabs Navigation -->', 0, start)
    if c_start != -1 and (start - c_start < 100):
        actual_start = c_start
    else:
        actual_start = start
    _, end = get_balanced_ul(html, start)
    widget = html[actual_start:end]
    html = html[:actual_start] + html[end:]
    return widget, html

# Extraction process
quick_info, html = extract_by_comment(html, '<!-- Quick Info Bar (Redesigned) -->')
assignment, html = extract_by_comment(html, '<!-- Assignment Card -->')

customers, html = extract_widget(html, "customers")
contacts, html = extract_widget(html, "contacts")
hardware, html = extract_widget(html, "hardware")
locations, html = extract_widget(html, "locations")
todo, html = extract_widget(html, "todo-steps")
wiki, html = extract_widget(html, "wiki")

reminders_tab_pane, html = extract_widget(html, "reminders")
# update the reminders tab pane wrapping to match right column styling
reminders_content = reminders_tab_pane.replace('class="tab-pane fade"', 'class="card right-module-card pt-1"').replace('id="reminders" role="tabpanel" tabindex="0"', '')
# Also remove reminders from the nav tab!
html = re.sub(r'<li class="nav-item"\s*role="presentation">\s*<button[^>]*data-bs-target="#reminders"[^>]*>.*?Påmindelser\s*</button>\s*</li>', '', html, flags=re.DOTALL)

sagsbeskrivelse, html = extract_by_comment(html, '<!-- ROW 1: Main Info -->')

nav_tabs, html = extract_ul_nav(html)

tab_content_start = html.find('<div class="tab-content" id="caseTabsContent">')
if tab_content_start != -1:
    tc_start, tc_end = get_balanced_div(html, tab_content_start)
    tab_content = html[tab_content_start:tc_end]
    html = html[:tab_content_start] + html[tc_end:]
else:
    tab_content = ""

# Strip old #details column wrapping
tab_content = tab_content.replace('<div class="row g-4">\n        <div class="col-lg-8" id="case-left-column">', '')
left_col_str = '<div class="col-lg-8" id="case-left-column">'
idx_l = tab_content.find(left_col_str)
if idx_l != -1:
    tab_content = tab_content[:idx_l] + tab_content[idx_l+len(left_col_str):]
    idx_row = tab_content.rfind('<div class="row g-4">', 0, idx_l)
    if idx_row != -1:
        tab_content = tab_content[:idx_row] + tab_content[idx_row+len('<div class="row g-4">'):]

right_col_str = '<div class="col-lg-4" id="case-right-column">'
idx_r = tab_content.find(right_col_str)
if idx_r != -1:
    r_start, r_end = get_balanced_div(tab_content, idx_r)
    tab_content = tab_content[:idx_r] + tab_content[r_end:]

# Since we removed 2 open divs (<div class="row g-4"><div class="col-lg-8...>), let's remove two nearest closing </div> before the end of the #details tab content
details_end = tab_content.find('<!-- Tab: Sagsdetaljer')
details_div_start = tab_content.find('<div class="tab-pane fade show active" id="details"')
details_div_end = get_balanced_div(tab_content, details_div_start)[1]

dt_content = tab_content[details_div_start:details_div_end]
# remove last two </div>
last_div = dt_content.rfind('</div>')
if last_div != -1:
    dt_content = dt_content[:last_div] + dt_content[last_div+6:]
last_div = dt_content.rfind('</div>')
if last_div != -1:
    dt_content = dt_content[:last_div] + dt_content[last_div+6:]

tab_content = tab_content[:details_div_start] + dt_content + tab_content[details_div_end:]

new_grid = f"""
{quick_info}

<div class="row g-4 mt-2">
    <!-- LEFT COLUMN: Kontekst & Stamdata -->
    <div class="col-xl-3 col-lg-4 order-2 order-xl-1">
        <h6 class="mb-3 text-muted text-uppercase fw-bold" style="font-size: 0.8rem; letter-spacing: 0.05em;">Kontekst & Stamdata</h6>
        <div class="d-flex flex-column gap-3">
            {customers}
            {contacts}
            {hardware}
            {locations}
            {wiki}
        </div>
    </div>
    
    <!-- MIDDLE COLUMN: Sagsbeskrivelse & Tabs -->
    <div class="col-xl-6 col-lg-8 order-1 order-xl-2">
        <div class="sticky-top bg-body pb-2" style="top: 0; z-index: 1020; margin-bottom: 1.5rem;">
            {sagsbeskrivelse}
        </div>
        
        {nav_tabs}
        
        <div class="pb-4">
            {tab_content}
        </div>
    </div>
    
    <!-- RIGHT COLUMN: Status, Tildeling, Todo, Påmindelser -->
    <div class="col-xl-3 col-lg-12 order-3 order-xl-3">
        <h6 class="mb-3 text-muted text-uppercase fw-bold" style="font-size: 0.8rem; letter-spacing: 0.05em;">Opsummering & Opgaver</h6>
        <div class="d-flex flex-column gap-3">
            {assignment}
            {todo}
            {reminders_content}
        </div>
    </div>
</div>
"""

top_bar_start = html.find('<!-- Top Bar:')
top_bar_div_start = html.find('<div', top_bar_start)
_, top_bar_div_end = get_balanced_div(html, top_bar_div_start)

final_html = html[:top_bar_div_end] + "\n" + new_grid + "\n" + html[top_bar_div_end:]

with open('app/modules/sag/templates/detail.html', 'w', encoding='utf-8') as f:
    f.write(final_html)

print("Done rewriting.")
