import os, glob, re, codecs

svgs = {
    'home': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m3 9 9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><polyline points="9 22 9 12 15 12 15 22"/></svg>',
    'flow': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="7" width="20" height="14" rx="2" ry="2"/><path d="M16 21V5a2 2 0 0 0-2-2h-4a2 2 0 0 0-2 2v16"/></svg>',
    'history': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>',
    'product': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/></svg>',
    'fashion-matches': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"/></svg>',
    'ai-report': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="20" x2="18" y2="10"/><line x1="12" y1="20" x2="12" y2="4"/><line x1="6" y1="20" x2="6" y2="14"/></svg>',
    'ai-sql': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/></svg>',
    'live-monitor': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 12h-4l-3 9L9 3l-3 9H2"/></svg>',
    'knowledge-graph': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><line x1="8.59" y1="13.51" x2="15.42" y2="17.49"/><line x1="15.41" y1="6.51" x2="8.59" y2="10.49"/></svg>',
    'reaction-simulator': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14.5 10c-.83 0-1.5-.67-1.5-1.5v-5c0-.83.67-1.5 1.5-1.5s1.5.67 1.5 1.5v5c0 .83-.67 1.5-1.5 1.5z"/><path d="M20.5 10H19V8.5c0-.83.67-1.5 1.5-1.5s1.5.67 1.5 1.5-.67 1.5-1.5 1.5z"/><path d="M9.5 14.5c.83 0 1.5 .67 1.5 1.5v5c0 .83-.67 1.5-1.5 1.5S8 21.83 8 21v-5c0-.83.67-1.5 1.5-1.5z"/><path d="M3.5 14H5v1.5c0 .83-.67 1.5-1.5 1.5S2 16.33 2 15.5 2.67 14 3.5 14z"/></svg>',
    'diagram-agent': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><line x1="3" y1="9" x2="21" y2="9"/><line x1="9" y1="21" x2="9" y2="21"/></svg>',
    'social-inbox': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>',
    'content-composer': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h9"/><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"/></svg>',
    'content-approval': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 11 12 14 22 4"/><path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"/></svg>',
    'content-calendar': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>',
    'media-library': '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="page-title-icon" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>',
}

html_files = glob.glob('D:/cnf/chatbot-canifa-feedback/backend/static/**/*.html', recursive=True)

for fpath in html_files:
    if "admin" in fpath or "main.html" in fpath or "dashboard" in fpath:
        continue
    folder = os.path.basename(os.path.dirname(fpath))
    file_name = os.path.basename(fpath).replace('.html', '')
    
    key = None
    if folder in svgs:
        key = folder
    elif file_name in svgs:
        key = file_name
    elif file_name == 'index' and folder == 'fashion-matches':
        key = 'fashion-matches'

    if key:
        with codecs.open(fpath, 'r', 'utf-8') as f:
            content = f.read()

        if '<svg' in content and 'page-title-icon' in content:
            continue

        match = re.search(r'(<h1[^>]*>)(.*?)(</h1>)', content, re.IGNORECASE)
        if match:
            h1_inner = match.group(2)
            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 { 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>')
            
            try:
                with codecs.open(fpath, 'w', 'utf-8') as f:
                    f.write(content)
                print(f"Updated {fpath}")
            except Exception as e:
                print(f"Write error on {fpath}")
