import os, glob, re, codecs

svgs = {
    'competitor-research': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="page-title-icon"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>',
    'dashboard-note': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="page-title-icon"><path d="M14.5 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7.5L14.5 2z"/><polyline points="14 2 14 8 20 8"/></svg>',
    'prompt-optimizer': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="page-title-icon"><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/></svg>',
    'regression-test': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="page-title-icon"><polyline points="20 6 9 17 4 12"/></svg>',
    'stress-test': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="page-title-icon"><path d="M13 2 3 14h9l-1 8 10-12h-9l1-8z"/></svg>',
    'user-simulator': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="page-title-icon"><rect x="2" y="3" width="20" height="14" rx="2" ry="2"/><line x1="8" y1="21" x2="16" y2="21"/><line x1="12" y1="17" x2="12" y2="21"/></svg>'
}

files = [
    'D:/cnf/chatbot-canifa-feedback/backend/static/competitor-research/competitor-research.html',
    'D:/cnf/chatbot-canifa-feedback/backend/static/dashboard-note/dashboard-note.html',
    'D:/cnf/chatbot-canifa-feedback/backend/static/prompt-optimizer/prompt-optimizer.html',
    'D:/cnf/chatbot-canifa-feedback/backend/static/regression-test/regression-test.html',
    'D:/cnf/chatbot-canifa-feedback/backend/static/stress-test/stress-test.html',
    'D:/cnf/chatbot-canifa-feedback/backend/static/user-simulator/user-simulator.html'
]

for fpath in files:
    key = os.path.basename(os.path.dirname(fpath))
    if key in svgs:
        try:
            with codecs.open(fpath, 'r', 'utf-8') as f:
                content = f.read()
                
            match = re.search(r'(<div\s+class="topbar"[^>]*>[\s\S]*?(?:<h1[^>]*>|<div\s+class="page-title"[^>]*>|<span\s+class="topbar-title"[^>]*>))(.*?)(</h1>|</div>|</span>)', content, re.IGNORECASE)
            
            if match:
                h1_inner = match.group(2)
                if '<svg' in h1_inner:
                    continue
                    
                clean_text = re.sub(r'^([^\w\s\<]+)', '', h1_inner).strip()
                new_h1_inner = f"{svgs[key]} {clean_text}"
                
                content = content.replace(f"{match.group(1)}{match.group(2)}{match.group(3)}", f"{match.group(1)}{new_h1_inner}{match.group(3)}")
                
                if '.page-title-icon' not in content:
                    content = content.replace('</head>', '    <style>\n    .topbar h1, .topbar .page-title, .topbar .topbar-title { display: flex; align-items: center; gap: 8px; }\n    .page-title-icon { width: 22px; height: 22px; color: var(--gold, #B45309); flex-shrink: 0; }\n    </style>\n</head>')
                
                with codecs.open(fpath, 'w', 'utf-8') as f:
                    f.write(content)
                print(f'Updated {fpath}')
        except Exception as e:
            print(f"Failed {fpath}: {e}")
