import os
import re

GALLERY_IMAGES = [
    "gallery_1_1785739465440.png",
    "gallery_2_1785739478020.png",
    "gallery_3_1785739488033.png",
    "gallery_4_1785739507745.png",
    "gallery_5_1785739518760.png",
    "gallery_6_1785739530049.png"
]

GRID_HTML = """
<!-- Gallery Grid Section -->
<section id="gallery-grid" style="padding: 5rem 2rem 5rem 2rem; background-color: #F1F1F1; box-sizing: border-box;">
    <div style="max-width: 1200px; margin: 0 auto;">
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 2.5rem;">
"""

for img in GALLERY_IMAGES:
    GRID_HTML += f"""
            <div style="overflow: hidden; border-radius: 24px; box-shadow: 0 10px 30px rgba(0,0,0,0.08); transition: transform 0.4s ease;">
                <img src="assets/images/{img}" alt="Church Gallery Image" style="width: 100%; height: 100%; object-fit: cover; aspect-ratio: 4/3; display: block; transition: transform 0.6s ease;" onmouseover="this.style.transform='scale(1.05)'" onmouseout="this.style.transform='scale(1)'">
            </div>
"""

GRID_HTML += """
        </div>
    </div>
</section>
"""

def update_index():
    with open('index.html', 'r', encoding='utf-8') as f:
        content = f.read()
    
    if 'id="gallery-grid"' not in content:
        # insert right before <footer>
        content = re.sub(r'(<footer)', GRID_HTML + r'\n        \1', content)
        
        with open('index.html', 'w', encoding='utf-8') as f:
            f.write(content)

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

    # Extract header and footer wrappers
    main_pattern = re.compile(r'(<main.*?>).*?(</main>)', re.DOTALL)
    
    HERO_HTML = """
        <!-- Gallery Hero Section -->
        <section style="position: relative; padding-top: 180px; padding-bottom: 120px; background: url('assets/images/church-bg.jpg') center/cover no-repeat;">
            <div style="position: absolute; inset: 0; background: rgba(20, 33, 58, 0.75);"></div>
            <div style="position: relative; z-index: 10; max-width: 1200px; margin: 0 auto; padding: 0 2rem; text-align: center;">
                <h1 style="font-family: 'Phudu', sans-serif !important; font-size: clamp(3rem, 5vw, 4.5rem); font-weight: 700; color: #fff; text-transform: uppercase; letter-spacing: -0.02em; margin-bottom: 1rem;">OUR GALLERY</h1>
                <div style="font-family: 'Instrument Sans', sans-serif; font-size: 1.05rem; font-weight: 600; color: #e7e2d6;">
                    Home / Image Gallery
                </div>
            </div>
        </section>
    """
    
    new_main = r'\1\n' + HERO_HTML + GRID_HTML + r'\n        \2'
    new_content = main_pattern.sub(new_main, content)
    
    # Update title
    new_content = re.sub(r'<title>.*?</title>', '<title>Image Gallery | The Franciscan Society</title>', new_content)
    
    with open('gallery.html', 'w', encoding='utf-8') as f:
        f.write(new_content)

update_index()
build_gallery_page()
print("Done")
