import os
import glob
import shutil
import re

def fix_bible_widget_css():
    css_file = os.path.join('css', 'bible-widget.css')
    with open(css_file, 'r', encoding='utf-8') as f:
        content = f.read()

    new_hover = """#bible-modal-close:hover {
    transform: rotate(90deg) scale(1.1) !important;
    background: transparent !important;
    color: #e6c888 !important;
    border-color: #e6c888 !important;
}"""
    
    # Use regex to replace the old hover block
    content = re.sub(
        r'#bible-modal-close:hover\s*\{[^}]*\}',
        new_hover,
        content
    )

    with open(css_file, 'w', encoding='utf-8') as f:
        f.write(content)
    print("Fixed bible-widget.css")

def setup_blogs_pages():
    # 1. Duplicate news.html to blogs.html
    if os.path.exists('news.html'):
        shutil.copy2('news.html', 'blogs.html')
        with open('blogs.html', 'r', encoding='utf-8') as f:
            content = f.read()
        
        # Replace titles and metadata
        content = content.replace('News & Events |', 'Blogs |')
        content = content.replace('content="News & Updates"', 'content="Blogs"')
        content = content.replace('NEWS & EVENTS', 'BLOGS')
        content = content.replace('news.html', 'blogs.html')
        content = content.replace('news-detail-', 'blogs-detail-')
        content = content.replace('News & Events', 'Blogs')
        
        with open('blogs.html', 'w', encoding='utf-8') as f:
            f.write(content)
        print("Created blogs.html")

    # 2. Rename blog-detail-*.html to blogs-detail-*.html and update links
    for i in range(1, 7):
        old_name = f'blog-detail-{i}.html'
        new_name = f'blogs-detail-{i}.html'
        if os.path.exists(old_name):
            os.rename(old_name, new_name)
        
        if os.path.exists(new_name):
            with open(new_name, 'r', encoding='utf-8') as f:
                content = f.read()
            
            # Replace breadcrumbs and URLs
            content = content.replace('news.html', 'blogs.html')
            content = content.replace('news-detail', 'blogs-detail')
            content = content.replace('News & Events', 'Blogs')
            
            with open(new_name, 'w', encoding='utf-8') as f:
                f.write(content)
            print(f"Renamed and updated {new_name}")

def update_index_html():
    with open('index.html', 'r', encoding='utf-8') as f:
        content = f.read()

    # 1. Update the "View All Blogs" button link
    # The button is around line 1649. Let's find it.
    # It looks like:
    # <!-- View All Blogs Button -->
    # <div class="gsap-fade-up" style="text-align: center; margin-top: 3rem;">
    #     <a href="news.html" class="btn-fill-animation">
    
    # Let's replace href="news.html" that comes immediately after <!-- View All Blogs Button -->
    pattern = r'(<!-- View All Blogs Button -->[\s\S]*?)href="news.html"'
    content = re.sub(pattern, r'\1href="blogs.html"', content)

    # 2. Update blog-detail- to blogs-detail-
    content = content.replace('href="blog-detail-', 'href="blogs-detail-')

    with open('index.html', 'w', encoding='utf-8') as f:
        f.write(content)
    print("Updated index.html")

if __name__ == '__main__':
    fix_bible_widget_css()
    setup_blogs_pages()
    update_index_html()
