import pathlib, re

templates_dir = pathlib.Path(r"D:\Bernardo\locadora_equipamentos\templates\dashboard")
# also base details?
all_html = list(templates_dir.glob("*.html"))
# include equipamento_detail etc already
for p in all_html:
    txt = p.read_text(encoding='utf-8')
    orig = txt

    # Fix broken backgrounds from previous run
    # Pattern: background:var(--surface) 0%, ... 100%);
    txt = re.sub(r'background:var\(--surface\)\s*0%[^;]*?100%\)\s*;', 'background:var(--surface);', txt)
    txt = re.sub(r'background:var\(--surface\)\s*0%[^;]*?100%\)\s*;?', 'background:var(--surface);', txt)
    # generic broken: background:var(--surface) 0%, #0369A1 100%);
    txt = re.sub(r'background:var\(--surface\)\s*0%,\s*#[0-9A-Fa-f]{6}\s*100%\)\s*;', 'background:var(--primary);', txt)
    # Fix double parentheses leftovers
    txt = txt.replace(" 100%);", ";")
    txt = txt.replace(" 0%, var(--page-bg) 100%);", ";")
    txt = txt.replace(" 0%, #0369A1 100%);", ";")
    txt = txt.replace(" 0%, #10B981 100%);", ";")
    txt = txt.replace(" 0%, #F59E0B 100%);", ";")
    # Now correct any remaining broken background fragments: look for "background:var(--surface) 0%"
    txt = re.sub(r'background:var\(--surface\)[^;]*?100%\)[^;]*;?', 'background:var(--surface);', txt)
    txt = re.sub(r'background:var\(--primary\)[^;]*?100%\)[^;]*;?', 'background:var(--primary);', txt)

    # Fix specific line in equipamentos.html: background:var(--surface) 0%, #0369A1 100%); already handled
    # More generic: fix "background:var(--surface) 0%" artifact
    txt = re.sub(r'background:var\(--(surface|primary|slate-100)\)\s+0%[^"]*?"', 'background:var(--\\1);"', txt)

    # Fix border-radius 16px -> header 12px, panel 8px
    # For page-header sections with 16px, change to 12px
    txt = txt.replace('border-radius:16px', 'border-radius:12px')
    # But after that, data-panel with 16px should be 8px; however we just set 16->12, but panel should be 8. Let's revert panels that were originally 16 but should be 8: they now 12, need to set to 8 for data-panel?
    # We'll change data-panel border-radius 12px where class is data-panel to 8px
    # Simple: if style contains data-panel and border-radius:12px, make 8px
    # Do generic: border-radius:12px for data-panel -> 8px
    # But we already changed 16->12, then we will change some 12->8 for panels
    # Identify panels: they have class="data-panel" with border-radius:12px
    # For now, change all remaining 12px that are in data-panel context to 8px, but keep page-header as 12
    # We'll do: if 'class="data-panel"' or 'id="novo' then radius 12->8
    # Instead, just change any border-radius:12px that is not page-header/welcome-row to 8px by checking line
    lines = txt.split("\n")
    new_lines = []
    for line in lines:
        if 'page-header' in line or 'welcome-row' in line:
            # keep 12px
            pass
        else:
            # change 12px -> 8px for panels/cards
            if 'border-radius:12px' in line and ('data-panel' in line or 'panel-toolbar' in line or 'projects-toolbar' in line):
                line = line.replace('border-radius:12px', 'border-radius:8px')
            elif 'border-radius:12px' in line and 'card' in line.lower():
                line = line.replace('border-radius:12px', 'border-radius:8px')
        new_lines.append(line)
    txt = "\n".join(new_lines)

    # Fix border strong to subtle in templates
    txt = txt.replace("var(--border-strong)", "var(--border-subtle)")
    txt = txt.replace("border:1px solid #E2E8F0", "border:1px solid var(--border-subtle)")
    txt = txt.replace("border:1px solid #E6EEF1", "border:1px solid var(--border-subtle)")
    txt = txt.replace("border:1px solid #E6EFF1", "border:1px solid var(--border-subtle)")
    txt = txt.replace("border:1px solid #CBD5E1", "border:1px solid var(--border-subtle)")
    txt = txt.replace("border:1px solid #BAE6FD", "border:1px solid var(--primary-border)") # keep primary border for active, but if spec says all subtle, keep? But primary border for active is okay
    # Actually spec says border sempre 1px solid var(--border-subtle) #E2E8F0, não 1.5px. So active borders should also be subtle? But we keep primary-border for primary soft pills? Let's keep primary-border where semantic primary, otherwise subtle
    # For now, ensure no 1.5px
    txt = txt.replace("1.5px solid", "1px solid")

    # Fix remaining linear-gradient strings (anywhere)
    # Replace linear-gradient(180deg, #0284C7 0%, #0369A1 100%) -> var(--primary)
    txt = re.sub(r'linear-gradient\(180deg,\s*#0284C7\s*0%,\s*#0369A1\s*100%\)', 'var(--primary)', txt)
    txt = re.sub(r'linear-gradient\(90deg,\s*#0284C7\s*0%,\s*#0369A1\s*100%\)', 'var(--primary)', txt)
    txt = re.sub(r'linear-gradient\(90deg,\s*#059669\s*0%,\s*#10B981\s*100%\)', 'var(--emerald)', txt)
    txt = re.sub(r'linear-gradient\(90deg,\s*#D97706\s*0%,\s*#F59E0B\s*100%\)', 'var(--safety-amber)', txt)
    txt = re.sub(r'linear-gradient\(90deg,\s*#D97706\s*0%,\s*#FBBF24\s*100%\)', 'var(--safety-amber)', txt)
    txt = re.sub(r'linear-gradient\(180deg,\s*#C4B5FD\s*0%,\s*#7C3AED\s*100%\)', 'var(--slate-600)', txt)
    txt = re.sub(r'linear-gradient\(180deg,\s*#FDE68A\s*0%,\s*#D97706\s*100%\)', 'var(--safety-amber)', txt)
    txt = re.sub(r'linear-gradient\(135deg,\s*#0B1120\s*0%,\s*#1E293B\s*100%\)', 'var(--slate-900)', txt)
    txt = re.sub(r'linear-gradient\(180deg,\s*#FFFBEB\s*0%,\s*#FEF3C7\s*100%\)', 'var(--surface)', txt)
    txt = re.sub(r'linear-gradient\(180deg,\s*#fff\s*0%,\s*var\(--page-bg\)\s*100%\)', 'var(--surface)', txt)
    txt = re.sub(r'linear-gradient\(180deg,\s*#F8FAFC\s*0%,\s*transparent\s*100%\)', 'var(--surface)', txt)
    txt = re.sub(r'linear-gradient\(180deg,\s*#[0-9A-Fa-f]{6}\s*0%,\s*#[0-9A-Fa-f]{6}\s*100%\)', 'var(--primary)', txt)
    txt = re.sub(r'linear-gradient\([^)]+\)', 'var(--surface)', txt) # fallback any remaining linear-gradient

    # Fix conic-gradient: keep but ensure palette restricted: replace violet in conic
    txt = txt.replace("#7C3AED", "#475569")
    txt = txt.replace("#8B5CF6", "#475569")
    txt = txt.replace("#A78BFA", "#64748B")
    txt = txt.replace("#C4B5FD", "#94A3B8")
    txt = txt.replace("#EDE9FE", "#F1F5F9")
    # Ensure conic still present for donut where needed: if we replaced conic with var(--surface) earlier for donut, we broke it. Restore donut conic for distribution charts
    # Distribution donut should be conic-gradient(#0284C7 0% {{ pct_disponivel }}%, #059669 ... )
    # Check if txt contains distribution but conic missing (we replaced with var(--surface))
    # For now, if file is equipamentos.html and contains distribution but no conic, restore
    if "conic-gradient" not in txt and "{{ pct_disponivel }}" in txt:
        txt = txt.replace("background:var(--surface); flex-shrink:0; border:2px solid var(--border-subtle); box-shadow:none;\">\n                <div style=\"position:absolute; inset:14px;", "background:conic-gradient(#0284C7 0% {{ pct_disponivel }}%, #059669 {{ pct_disponivel }}% {{ pct_disponivel|add:pct_uso }}%, #E2E8F0 {{ pct_disponivel|add:pct_uso }}% 100%); flex-shrink:0; border:2px solid var(--border-subtle); box-shadow:none;\">\n                <div style=\"position:absolute; inset:14px;", 1)
    # For index donut similar already present

    # Fix volt donut in equipamentos.html: currently background:var(--slate-100) should be conic with primary/success/slate
    if 'Distribuição elétrica' in txt:
        txt = txt.replace('background:var(--slate-100); flex-shrink:0; border:2px solid var(--border-subtle); box-shadow:none;">\n                <div style="position:absolute; inset:14px; background:var(--surface);', 'background:conic-gradient(#0284C7 0% 30%, #059669 30% 60%, #E2E8F0 60% 100%); flex-shrink:0; border:2px solid var(--border-subtle); box-shadow:none;">\n                <div style="position:absolute; inset:14px; background:var(--surface);')

    # Fix location bar background that got broken: background:var(--surface) 0%, #0369A1 100%)
    txt = re.sub(r'background:var\(--surface\)[^;]*?#0369A1[^;]*?;', 'background:var(--primary);', txt)

    # Fix font weights border radius etc already
    # Ensure status pills have max 1 color: ensure not multiple colors in same component
    # For card-icon violet already neutralized

    # Fix emojis still left: check for any remaining non-ascii icon symbols that are emojis
    # Replace ↓ and ⌄ and → already but check for "⌄" (U+2304) used for filter dropdown
    txt = txt.replace("⌄", '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" style="display:inline-block; vertical-align:middle;"><polyline points="6 9 12 15 18 9"></polyline></svg>')
    txt = txt.replace("↓", '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" style="display:inline-block; vertical-align:middle;"><line x1="12" y1="5" x2="12" y2="19"></line><polyline points="19 12 12 19 5 12"></polyline></svg>')
    txt = txt.replace("↗", '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" style="display:inline-block; vertical-align:middle;"><polyline points="23 6 13.5 15.5 8.5 10.5 1 18"></polyline><polyline points="17 6 23 6 23 12"></polyline></svg>')
    txt = txt.replace("→", '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" style="display:inline-block; vertical-align:middle;"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>')
    # Remove double arrow leftovers "→" already

    # Fix whitespace scale: ensure gap 24 for sections, 16 for cards
    # Already done

    # Fix height 6px glow etc already

    if txt != orig:
        p.write_text(txt, encoding='utf-8')
        print(f"fixed {p.name}")
    else:
        print(f"no change {p.name}")

# Also fix base.css remaining broken? Check for broken background fragments
import pathlib as pl
base = pl.Path(r"D:\Bernardo\locadora_equipamentos\static\css\base.css")
txt = base.read_text(encoding='utf-8')
orig = txt
# remove any leftover linear-gradient in base.css properties (should be none, but selectors with linear-gradient in attribute selectors are ok)
txt = re.sub(r'background:\s*linear-gradient\([^)]+\)', 'background:var(--surface)', txt)
if txt != orig:
    base.write_text(txt, encoding='utf-8')
    print("base.css extra fixed")

