import pathlib, re
templates_dir = pathlib.Path(r"D:\Bernardo\locadora_equipamentos\templates\dashboard")
for fname in ["equipamentos.html","equipamentos_em_obra.html","os.html","index.html","obras.html","manutencoes.html"]:
    p = templates_dir / fname
    txt = p.read_text(encoding='utf-8')
    orig = txt
    # Fix progress bars: var(--surface) where should be var(--primary)
    # equipamentos.html location bar
    txt = txt.replace('background:var(--surface); border-radius:6px;"></div></div>', 'background:var(--primary); border-radius:6px;"></div></div>')
    # But need to distinguish: in equipamentos.html there are two similar lines, one is l.total location bar, other category bar
    # For location bar specifically: width:{% widthratio l.total
    txt = re.sub(r'(\{% widthratio l\.total[^%]+%}; background:)var\(--surface\)', r'\1var(--primary)', txt)
    txt = re.sub(r'(\{% widthratio cl\.total[^%]+%}; background:)var\(--surface\)', r'\1var(--primary)', txt)
    # For os.html progress middle case var(--surface) -> var(--primary)
    txt = txt.replace("{% elif os.progress > 50 %}var(--surface)", "{% elif os.progress > 50 %}var(--primary)")
    # For equipamentos item availability bar danger color #E11D48 -> #DC2626
    txt = txt.replace("#E11D48", "#DC2626")
    txt = txt.replace("#E11D48".lower(), "#DC2626")
    # Fix category cycle multi-color -> single primary
    txt = re.sub(r"\{% cycle 'var\(--primary\)' '#475569' '#D97706' '#059669'[^%]*%}", "{% cycle 'var(--primary)' 'var(--primary)' 'var(--primary)' 'var(--primary)' %}", txt)
    # More generic: any cycle with multiple colors replace with primary
    txt = re.sub(r"\{% cycle[^%]*%}", "var(--primary)", txt) # simpler: just replace cycle block with var(--primary) inside background:?
    # But we replaced already, need to ensure background stays var(--primary)
    # For category bar: background:{% cycle ... %}; -> background:var(--primary);
    txt = re.sub(r"background:\{% cycle[^}]+\};", "background:var(--primary);", txt)
    txt = re.sub(r"background:\{% cycle[^}]+\}", "background:var(--primary)", txt)

    # Fix remaining linear-gradient fragments that might have been missed
    txt = re.sub(r'linear-gradient\([^)]+\)', 'var(--primary)', txt)

    # Ensure border subtle
    txt = txt.replace("var(--border-strong)", "var(--border-subtle)")

    # Ensure no purple left
    txt = txt.replace("#475569", "#475569") # keep as slate, okay
    # Ensure amber used only for warning states: keep

    # Fix width 6px dots already but check for 6px height with background var(--primary) etc should be 8px
    # Already done

    # Fix font weights for KPI still 800? Ensure strong KPI 1.5rem is 700 tabular-nums
    # In manutencoes.html, strong still 1.5rem but we fixed global? Check manually later

    # Ensure all inputs have radius 6px already

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

# also fix colaborador etc for category bars if any
for p in templates_dir.glob("*.html"):
    txt = p.read_text(encoding='utf-8')
    if "{% cycle" in txt:
        orig = txt
        txt = re.sub(r"\{% cycle[^%]*%}", "var(--primary)", txt)
        txt = txt.replace("background:var(--primary) ;", "background:var(--primary);")
        if txt != orig:
            p.write_text(txt, encoding='utf-8')
            print(f"cycle fixed {p.name}")

# Fix base.css: ensure danger color #DC2626 not #E11D48
base = pathlib.Path(r"D:\Bernardo\locadora_equipamentos\static\css\base.css")
txt = base.read_text(encoding='utf-8')
orig = txt
txt = txt.replace("#E11D48", "#DC2626")
# Ensure shadows xs .06
txt = txt.replace("rgba(15, 23, 42, 0.08)", "rgba(15,23,42,.06)")
txt = txt.replace("rgba(15,23,42,0.08)", "rgba(15,23,42,.06)")
if txt != orig:
    base.write_text(txt, encoding='utf-8')
    print("base fixed2")
