import re, sys

path = '/home/szymon/projects/tfb/app/panel/views/analysis/revenue_dashboard.twig'
with open(path, 'r', encoding='utf-8') as f:
    content = f.read()

# ─── 1. CSS: stara sekcja tabeli -> kompaktowa ────────────────────────────────
OLD_CSS = (
    "    /* \u2500\u2500 Tabela \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */\n"
    "    #rd-table { font-size: 0.8rem; }\n"
    "    #rd-table thead th { font-size: 0.7rem; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--rd-t2); background: #f8f8f6; white-space: nowrap; }\n"
    "    #rd-table tbody tr { transition: background .1s; }\n"
    "    #rd-table tbody tr:hover { background: #fafaf8; }\n"
    "    #rd-table tbody tr.rd-row-partial { opacity: .55; }\n"
    "    #rd-table tbody tr.rd-row-total { background: #f0ede9; font-weight: 700; border-top: 2px solid #ddd; }\n"
    "    .rd-cost-bar { display: flex; align-items: center; gap: 5px; justify-content: flex-end; }\n"
    "    .rd-bar { height: 4px; border-radius: 2px; }\n"
    "    .rd-pos { color: var(--rd-ok); font-weight: 600; }\n"
    "    .rd-neg { color: var(--rd-dg); font-weight: 600; }\n"
    "    .rd-neu { color: var(--rd-t3); }\n"
    "    .rd-ptag { display: inline-block; padding: 1px 5px; border-radius: 3px; font-size: 0.65rem; background: var(--rd-sf3); color: var(--rd-t3); margin-left: 3px; vertical-align: middle; }"
)
NEW_CSS = (
    "    /* \u2500\u2500 Tabela \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */\n"
    "    #rd-table { font-size: 0.68rem; table-layout: fixed; width: 100%; }\n"
    "    #rd-table thead th {\n"
    "        font-size: 0.6rem; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;\n"
    "        color: var(--rd-t2); background: #f8f8f6;\n"
    "        white-space: normal; word-break: break-word;\n"
    "        padding: 5px 4px; text-align: center; vertical-align: bottom;\n"
    "    }\n"
    "    #rd-table td { padding: 4px 5px; vertical-align: middle; white-space: nowrap; }\n"
    "    #rd-table tbody tr { transition: background .1s; }\n"
    "    #rd-table tbody tr:hover { background: #fafaf8; }\n"
    "    #rd-table tbody tr.rd-row-partial { opacity: .55; }\n"
    "    #rd-table tbody tr.rd-row-total { background: #f0ede9; font-weight: 700; border-top: 2px solid #ddd; }\n"
    "    /* szerokosci kolumn */\n"
    "    #rd-table th:nth-child(1)  { width: 7%; }\n"
    "    #rd-table th:nth-child(2)  { width: 6%; }\n"
    "    #rd-table th:nth-child(3)  { width: 5%; }\n"
    "    #rd-table th:nth-child(4)  { width: 12%; }\n"
    "    #rd-table th:nth-child(5)  { width: 8%; }\n"
    "    #rd-table th:nth-child(6)  { width: 5%; }\n"
    "    #rd-table th:nth-child(7)  { width: 12%; }\n"
    "    #rd-table th:nth-child(8)  { width: 12%; }\n"
    "    #rd-table th:nth-child(9)  { width: 9%; }\n"
    "    #rd-table th:nth-child(10) { width: 14%; }\n"
    "    #rd-table th:nth-child(11) { width: 10%; }\n"
    "    .rd-cost-inline { display: flex; align-items: center; gap: 4px; justify-content: flex-end; }\n"
    "    .rd-bar { height: 3px; border-radius: 2px; flex-shrink: 0; }\n"
    "    .rd-pos { color: var(--rd-ok); font-weight: 600; }\n"
    "    .rd-neg { color: var(--rd-dg); font-weight: 600; }\n"
    "    .rd-neu { color: var(--rd-t3); }\n"
    "    .rd-ptag { display: inline-block; padding: 1px 4px; border-radius: 3px; font-size: 0.58rem; background: var(--rd-sf3); color: var(--rd-t3); vertical-align: middle; }\n"
    "    .rd-sub { font-size: 0.6rem; color: var(--rd-t3); }"
)

if OLD_CSS in content:
    content = content.replace(OLD_CSS, NEW_CSS, 1)
    print("Step 1 (CSS) OK")
else:
    print("WARN: old CSS block not found by exact match, trying regex...")
    m = re.search(r"    /\* \u2500\u2500 Tabela.*?\.rd-ptag \{[^}]+\}", content, re.DOTALL)
    if m:
        content = content[:m.start()] + NEW_CSS + content[m.end():]
        print("Step 1 (CSS via regex) OK")
    else:
        sys.exit("ERROR: CSS block not found")

# ─── 2. Usun zduplikowany fmtK (stary z currency suffix) ─────────────────────
OLD_FMTK_DUP = (
    "const fmtK = n => {\n"
    "    const v = Number(n ?? 0);\n"
    "    if (Math.abs(v) >= 1000) return (v / 1000).toFixed(1) + 'k ' + RD.currency;\n"
    "    return v.toFixed(0) + ' ' + RD.currency;\n"
    "};\n"
)
if OLD_FMTK_DUP in content:
    content = content.replace(OLD_FMTK_DUP, '', 1)
    print("Step 2 (remove duplicate fmtK) OK")
else:
    # try regex
    m2 = re.search(r"const fmtK = n => \{[^}]+\};\n", content, re.DOTALL)
    if m2:
        # check if there are 2 fmtK definitions
        all_fmtk = list(re.finditer(r'const fmtK\b', content))
        if len(all_fmtk) > 1:
            # remove the second one
            pat_dup = re.search(r'const fmtK = n =>[^\n]*\n.*?\};\n', content, re.DOTALL)
            if pat_dup:
                content = content[:pat_dup.start()] + content[pat_dup.end():]
                print("Step 2 (remove duplicate fmtK via regex) OK")
        else:
            print("Step 2: only one fmtK found, skipping")
    else:
        print("Step 2: duplicate fmtK not found, skipping")

# ─── 3. Napraw zla encje HTML &#141; -> prawidlowe Ł (UTF-8) ─────────────────
if '&#141;adowanie' in content:
    content = content.replace('&#141;adowanie', '\u0141adowanie', 1)
    print("Step 3 (fix Ł entity) OK")
else:
    print("Step 3: entity &#141; not found or already correct")

with open(path, 'w', encoding='utf-8') as f:
    f.write(content)

print("ALL DONE")

