#!/usr/bin/env python3
"""Rebranding script for Krzysztof Jackowski platform - Wizja"""
import os, re, glob

DIR = os.path.dirname(os.path.abspath(__file__))
files = sorted(glob.glob(os.path.join(DIR, '*.html')))

# New header nav (14 items for Jackowski platform)
NEW_HEADER_NAV_ITEMS = [
    ('home-full.html', 'solar:feed-linear'),
    ('profile.html', 'solar:user-circle-linear'),
    ('groups.html', 'solar:users-group-rounded-linear'),
    ('messages.html', 'solar:chat-round-dots-linear'),
    ('members.html', 'solar:user-plus-linear'),
    ('forums.html', 'solar:chat-line-linear'),
    ('courses.html', 'solar:square-academic-cap-linear'),
    ('events.html', 'solar:videocamera-record-linear'),  # Wizje Na Żywo
    ('predictions.html', 'solar:eye-linear'),  # NEW
    ('vision-diary.html', 'solar:notebook-linear'),  # NEW
    ('spiritual-matching.html', 'solar:heart-linear'),  # NEW
    ('meditations.html', 'solar:meditation-round-linear'),  # NEW
    ('blog.html', 'solar:document-text-linear'),
    ('shop.html', 'solar:bag-4-linear'),
]

# Map filenames to their active header link
ACTIVE_MAP = {
    'home-full.html': 'home-full.html',
    'profile.html': 'profile.html',
    'groups.html': 'groups.html',
    'group-detail.html': 'groups.html',
    'messages.html': 'messages.html',
    'members.html': 'members.html',
    'forums.html': 'forums.html',
    'courses.html': 'courses.html',
    'course-detail.html': 'courses.html',
    'events.html': 'events.html',
    'predictions.html': 'predictions.html',
    'vision-diary.html': 'vision-diary.html',
    'spiritual-matching.html': 'spiritual-matching.html',
    'meditations.html': 'meditations.html',
    'blog.html': 'blog.html',
    'blog-detail.html': 'blog.html',
    'shop.html': 'shop.html',
    'photos.html': 'home-full.html',
    'video.html': 'home-full.html',
    'notifications.html': 'home-full.html',
    'settings.html': 'profile.html',
}

def build_header_nav(active_href):
    lines = []
    for href, icon in NEW_HEADER_NAV_ITEMS:
        if href == active_href:
            cls = 'h-full flex items-center border-b-[2px] border-[#6D28D9] px-3 text-[#6D28D9]'
        else:
            cls = 'h-full flex items-center border-b-[2px] border-transparent px-3 text-slate-400 hover:text-slate-600 dark:hover:text-slate-200 transition-colors'
        lines.append(f'                <a href="{href}" class="{cls}"><iconify-icon icon="{icon}" width="24" height="24"></iconify-icon></a>')
    return '\n'.join(lines)

# Sidebar link helper
SB_NORMAL = 'flex items-center gap-3 px-3 py-2 text-sm font-medium text-slate-600 dark:text-slate-400 rounded-md hover:bg-gray-50 dark:hover:bg-white/5 hover:text-slate-900 dark:hover:text-slate-100 transition-colors'
SB_ACTIVE = 'flex items-center gap-3 px-3 py-2 text-sm font-medium text-[#6D28D9] bg-[#6D28D9]/10 rounded-md'

SIDEBAR_LINKS = [
    # (section_title, [(href, icon, label), ...])
    ('Osobiste', [
        ('profile.html', 'solar:user-circle-linear', 'Mój profil'),
        ('home-full.html', 'solar:graph-up-linear', 'Oś czasu'),
        ('messages.html', 'solar:inbox-linear', 'Wiadomości'),
        ('notifications.html', 'solar:bell-linear', 'Powiadomienia'),
    ]),
    ('Społeczność', [
        ('groups.html', 'solar:users-group-rounded-linear', 'Grupy Tematyczne'),
        ('members.html', 'solar:user-plus-linear', 'Członkowie'),
        ('forums.html', 'solar:chat-line-linear', 'Dyskusje'),
        ('events.html', 'solar:videocamera-record-linear', 'Wizje Na Żywo'),
        ('courses.html', 'solar:diploma-linear', 'Kursy'),
    ]),
    ('Wizje & Duchowość', [
        ('predictions.html', 'solar:eye-linear', 'Baza Przepowiedni'),
        ('vision-diary.html', 'solar:notebook-linear', 'Dziennik Wizji'),
        ('spiritual-matching.html', 'solar:heart-linear', 'Randki Duchowe'),
        ('meditations.html', 'solar:meditation-round-linear', 'Medytacje'),
    ]),
    ('Multimedia', [
        ('blog.html', 'solar:document-text-linear', 'Blog'),
        ('video.html', 'solar:videocamera-linear', 'Wideo'),
        ('photos.html', 'solar:gallery-linear', 'Zdjęcia'),
    ]),
    ('Sklep', [
        ('shop.html', 'solar:bag-4-linear', 'Sklep Wizja'),
    ]),
]

# Map filename to active sidebar link
SIDEBAR_ACTIVE_MAP = {
    'home-full.html': 'home-full.html',
    'profile.html': 'profile.html',
    'groups.html': 'groups.html',
    'group-detail.html': 'groups.html',
    'messages.html': 'messages.html',
    'members.html': 'members.html',
    'forums.html': 'forums.html',
    'courses.html': 'courses.html',
    'course-detail.html': 'courses.html',
    'events.html': 'events.html',
    'predictions.html': 'predictions.html',
    'vision-diary.html': 'vision-diary.html',
    'spiritual-matching.html': 'spiritual-matching.html',
    'meditations.html': 'meditations.html',
    'blog.html': 'blog.html',
    'blog-detail.html': 'blog.html',
    'shop.html': 'shop.html',
    'photos.html': 'photos.html',
    'video.html': 'video.html',
    'notifications.html': 'notifications.html',
    'settings.html': 'profile.html',
}

def build_sidebar(active_href):
    sections = []
    for title, links in SIDEBAR_LINKS:
        items = []
        items.append(f'                    <div class="px-3 mb-2 text-xs font-semibold text-slate-400 dark:text-slate-500 uppercase tracking-wider">{title}</div>')
        for href, icon, label in links:
            cls = SB_ACTIVE if href == active_href else SB_NORMAL
            if href == 'messages.html' and active_href != 'messages.html':
                # Special: messages has badge
                items.append(f'                    <a href="{href}" class="flex items-center justify-between px-3 py-2 text-sm font-medium text-slate-600 dark:text-slate-400 rounded-md hover:bg-gray-50 dark:hover:bg-white/5 hover:text-slate-900 dark:hover:text-slate-100 transition-colors"><div class="flex items-center gap-3"><iconify-icon icon="{icon}" width="20" height="20"></iconify-icon>{label}</div><span class="bg-gray-100 dark:bg-slate-800 text-slate-500 dark:text-slate-400 text-xs font-semibold px-1.5 py-0.5 rounded">3</span></a>')
            elif href == 'messages.html' and active_href == 'messages.html':
                items.append(f'                    <a href="{href}" class="flex items-center justify-between px-3 py-2 text-sm font-medium text-[#6D28D9] bg-[#6D28D9]/10 rounded-md"><div class="flex items-center gap-3"><iconify-icon icon="{icon}" width="20" height="20"></iconify-icon>{label}</div><span class="bg-violet-100 dark:bg-violet-800 text-[#6D28D9] text-xs font-semibold px-1.5 py-0.5 rounded">3</span></a>')
            else:
                items.append(f'                    <a href="{href}" class="{cls}"><iconify-icon icon="{icon}" width="20" height="20"></iconify-icon>{label}</a>')
        sections.append('                <div class="space-y-1">\n' + '\n'.join(items) + '\n                </div>')
    return '\n'.join(sections)

# New logo HTML with crystal ball emoji
NEW_LOGO = '''<a href="home-full.html" class="flex items-center gap-2.5">
                <div class="w-8 h-8 rounded-full bg-[#6D28D9] flex items-center justify-center text-white text-lg">🔮</div>
                <div class="flex flex-col"><span class="font-bold text-slate-900 dark:text-slate-100 tracking-tight leading-none text-sm">Wizja</span><span class="font-medium text-slate-500 text-xs tracking-tight">Społeczność Jackowskiego</span></div>
            </a>'''

print('🔮 Rebranding platform for Krzysztof Jackowski...\n')

for fpath in files:
    fname = os.path.basename(fpath)
    print(f'Processing: {fname}')
    
    with open(fpath, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # 1. Replace brand color #2D8F4E → #6D28D9
    content = content.replace('#2D8F4E', '#6D28D9')
    
    # 2. Replace emerald references to violet in Tailwind classes
    content = content.replace('emerald-600', 'violet-700')
    content = content.replace('emerald-500', 'violet-600')
    content = content.replace('emerald-400', 'violet-500')
    content = content.replace('emerald-50', 'violet-50')
    content = content.replace('bg-emerald-500/10', 'bg-violet-700/10')
    
    # 3. Replace tailwind config brand color
    content = content.replace("brand: { green: '#2D8F4E'", "brand: { violet: '#6D28D9'")
    
    # 4. Replace title
    content = re.sub(r'<title>(.*?) — .*?</title>', 
                     lambda m: f'<title>{m.group(1)} — Wizja | Społeczność Jackowskiego</title>', content)
    
    # 5. Replace logo block
    logo_pattern = r'<a href="home-full\.html" class="flex items-center gap-2\.5">.*?</a>'
    content = re.sub(logo_pattern, NEW_LOGO, content, flags=re.DOTALL, count=1)
    
    # 6. Replace header nav
    active_header = ACTIVE_MAP.get(fname, 'home-full.html')
    new_nav = build_header_nav(active_header)
    nav_pattern = r'(<nav class="hidden md:flex items-center h-full gap-1">).*?(</nav>)'
    content = re.sub(nav_pattern, f'\\1\n{new_nav}\n            \\2', content, flags=re.DOTALL)
    
    # 7. Replace sidebar nav
    active_sidebar = SIDEBAR_ACTIVE_MAP.get(fname, 'home-full.html')
    new_sidebar = build_sidebar(active_sidebar)
    sidebar_pattern = r'(<nav class="flex-1 px-4 space-y-8">).*?(</nav>)'
    content = re.sub(sidebar_pattern, f'\\1\n{new_sidebar}\n            \\2', content, flags=re.DOTALL)
    
    with open(fpath, 'w', encoding='utf-8') as f:
        f.write(content)
    
    print(f'  ✓ Done: {fname}')

print('\n✅ Rebranding complete! Platform is now branded as "Wizja | Społeczność Jackowskiego"')
print('🔮 Crystal ball logo, violet color (#6D28D9), 14 nav items, 5 sidebar sections ready!')
