import os
import re

# 1. Fix community-friaries.html layout
friaries_html = 'community-friaries.html'
with open(friaries_html, 'r', encoding='utf-8') as f:
    c = f.read()

# We will replace the single item dioceses with elegant side-by-side grouped grids
old_single_dioceses_block = """        <h3 style="font-family: 'Phudu', sans-serif; font-size: 1.5rem; font-weight: 700; color: #4a2a18; margin-top: 3rem; margin-bottom: 1.5rem; border-bottom: 2px solid #e5e5e5; padding-bottom: 0.5rem;">DIOCESE OF GUMLA</h3>
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 2rem; margin-bottom: 2rem;">
            <div style="border-radius: 12px; overflow: hidden; box-shadow: 0 10px 20px rgba(0,0,0,0.05); display: flex; flex-direction: column; background: #fff;">
                <img loading="lazy" decoding="async" src="assets/images/gallery/IMG_2531.JPG" alt="Franciscan Ashram Murumkela" style="width: 100%; height: 200px; object-fit: cover;">
                <div style="padding: 1.5rem;">
                    <h4 style="font-family: 'Phudu', sans-serif; font-size: 1.1rem; font-weight: 700; color: #1c1917; margin-top: 0; margin-bottom: 0.5rem;">Franciscan Ashram Murumkela</h4>
                    <p style="font-family: 'Instrument Sans', sans-serif; font-size: 0.95rem; color: #57534e; line-height: 1.5; margin: 0;">P.O. Kansir (Via – Chainpur), Gumla – 835206, JHARKHAND, Estd. 1998</p>
                </div>
            </div>
        </div>
        <h3 style="font-family: 'Phudu', sans-serif; font-size: 1.5rem; font-weight: 700; color: #4a2a18; margin-top: 3rem; margin-bottom: 1.5rem; border-bottom: 2px solid #e5e5e5; padding-bottom: 0.5rem;">DIOCESE OF PURNEA</h3>
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 2rem; margin-bottom: 2rem;">
            <div style="border-radius: 12px; overflow: hidden; box-shadow: 0 10px 20px rgba(0,0,0,0.05); display: flex; flex-direction: column; background: #fff;">
                <img loading="lazy" decoding="async" src="assets/images/gallery/WhatsApp Image 2026-08-10 at 2.17.28 PM (1).jpeg" alt="Franciscan Ashram Thakurganj" style="width: 100%; height: 200px; object-fit: cover;">
                <div style="padding: 1.5rem;">
                    <h4 style="font-family: 'Phudu', sans-serif; font-size: 1.1rem; font-weight: 700; color: #1c1917; margin-top: 0; margin-bottom: 0.5rem;">Franciscan Ashram Thakurganj</h4>
                    <p style="font-family: 'Instrument Sans', sans-serif; font-size: 0.95rem; color: #57534e; line-height: 1.5; margin: 0;">P.O. Thakurganj, Kishanganj – 855116, BIHAR, Estd. 1996</p>
                </div>
            </div>
        </div>
        <h3 style="font-family: 'Phudu', sans-serif; font-size: 1.5rem; font-weight: 700; color: #4a2a18; margin-top: 3rem; margin-bottom: 1.5rem; border-bottom: 2px solid #e5e5e5; padding-bottom: 0.5rem;">DIOCESE OF BAGDOGRA</h3>
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 2rem; margin-bottom: 2rem;">
            <div style="border-radius: 12px; overflow: hidden; box-shadow: 0 10px 20px rgba(0,0,0,0.05); display: flex; flex-direction: column; background: #fff;">
                <img loading="lazy" decoding="async" src="assets/images/gallery/WhatsApp Image 2026-08-10 at 2.17.26 PM.jpeg" alt="Franciscan Ashram Adhikari" style="width: 100%; height: 200px; object-fit: cover;">
                <div style="padding: 1.5rem;">
                    <h4 style="font-family: 'Phudu', sans-serif; font-size: 1.1rem; font-weight: 700; color: #1c1917; margin-top: 0; margin-bottom: 0.5rem;">Franciscan Ashram Adhikari</h4>
                    <p style="font-family: 'Instrument Sans', sans-serif; font-size: 0.95rem; color: #57534e; line-height: 1.5; margin: 0;">P.O. Adhikari (Via – Khoribari), Darjeeling – 734427, WEST BENGAL, Estd. 1982.</p>
                </div>
            </div>
        </div>"""

new_grouped_dioceses_block = """        <!-- Single Friary Dioceses Grouped in a 3-Column Row -->
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 2rem; margin-top: 3rem; margin-bottom: 2rem; align-items: start;">
            <!-- Diocese of Gumla -->
            <div>
                <h3 style="font-family: 'Phudu', sans-serif; font-size: 1.35rem; font-weight: 700; color: #4a2a18; margin-top: 0; margin-bottom: 1.2rem; border-bottom: 2px solid #e5e5e5; padding-bottom: 0.5rem;">DIOCESE OF GUMLA</h3>
                <div style="border-radius: 12px; overflow: hidden; box-shadow: 0 10px 20px rgba(0,0,0,0.05); display: flex; flex-direction: column; background: #fff; height: 100%;">
                    <img loading="lazy" decoding="async" src="assets/images/gallery/IMG_2531.JPG" alt="Franciscan Ashram Murumkela" style="width: 100%; height: 200px; object-fit: cover;">
                    <div style="padding: 1.5rem; flex-grow: 1;">
                        <h4 style="font-family: 'Phudu', sans-serif; font-size: 1.1rem; font-weight: 700; color: #1c1917; margin-top: 0; margin-bottom: 0.5rem;">Franciscan Ashram Murumkela</h4>
                        <p style="font-family: 'Instrument Sans', sans-serif; font-size: 0.95rem; color: #57534e; line-height: 1.5; margin: 0;">P.O. Kansir (Via – Chainpur), Gumla – 835206, JHARKHAND, Estd. 1998</p>
                    </div>
                </div>
            </div>

            <!-- Diocese of Purnea -->
            <div>
                <h3 style="font-family: 'Phudu', sans-serif; font-size: 1.35rem; font-weight: 700; color: #4a2a18; margin-top: 0; margin-bottom: 1.2rem; border-bottom: 2px solid #e5e5e5; padding-bottom: 0.5rem;">DIOCESE OF PURNEA</h3>
                <div style="border-radius: 12px; overflow: hidden; box-shadow: 0 10px 20px rgba(0,0,0,0.05); display: flex; flex-direction: column; background: #fff; height: 100%;">
                    <img loading="lazy" decoding="async" src="assets/images/gallery/WhatsApp Image 2026-08-10 at 2.17.28 PM (1).jpeg" alt="Franciscan Ashram Thakurganj" style="width: 100%; height: 200px; object-fit: cover;">
                    <div style="padding: 1.5rem; flex-grow: 1;">
                        <h4 style="font-family: 'Phudu', sans-serif; font-size: 1.1rem; font-weight: 700; color: #1c1917; margin-top: 0; margin-bottom: 0.5rem;">Franciscan Ashram Thakurganj</h4>
                        <p style="font-family: 'Instrument Sans', sans-serif; font-size: 0.95rem; color: #57534e; line-height: 1.5; margin: 0;">P.O. Thakurganj, Kishanganj – 855116, BIHAR, Estd. 1996</p>
                    </div>
                </div>
            </div>

            <!-- Diocese of Bagdogra -->
            <div>
                <h3 style="font-family: 'Phudu', sans-serif; font-size: 1.35rem; font-weight: 700; color: #4a2a18; margin-top: 0; margin-bottom: 1.2rem; border-bottom: 2px solid #e5e5e5; padding-bottom: 0.5rem;">DIOCESE OF BAGDOGRA</h3>
                <div style="border-radius: 12px; overflow: hidden; box-shadow: 0 10px 20px rgba(0,0,0,0.05); display: flex; flex-direction: column; background: #fff; height: 100%;">
                    <img loading="lazy" decoding="async" src="assets/images/gallery/WhatsApp Image 2026-08-10 at 2.17.26 PM.jpeg" alt="Franciscan Ashram Adhikari" style="width: 100%; height: 200px; object-fit: cover;">
                    <div style="padding: 1.5rem; flex-grow: 1;">
                        <h4 style="font-family: 'Phudu', sans-serif; font-size: 1.1rem; font-weight: 700; color: #1c1917; margin-top: 0; margin-bottom: 0.5rem;">Franciscan Ashram Adhikari</h4>
                        <p style="font-family: 'Instrument Sans', sans-serif; font-size: 0.95rem; color: #57534e; line-height: 1.5; margin: 0;">P.O. Adhikari (Via – Khoribari), Darjeeling – 734427, WEST BENGAL, Estd. 1982.</p>
                    </div>
                </div>
            </div>
        </div>"""

c = c.replace(old_single_dioceses_block, new_grouped_dioceses_block)

# Also fix Diocese of Bongaigaon single card so it doesn't stretch full width:
old_bongaigaon = """        <h3 style="font-family: 'Phudu', sans-serif; font-size: 1.5rem; font-weight: 700; color: #4a2a18; margin-top: 3rem; margin-bottom: 1.5rem; border-bottom: 2px solid #e5e5e5; padding-bottom: 0.5rem;">DIOCESE OF BONGAIGAON</h3>
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 2rem; margin-bottom: 2rem;">
            <div style="border-radius: 12px; overflow: hidden; box-shadow: 0 10px 20px rgba(0,0,0,0.05); display: flex; flex-direction: column; background: #fff;">
                <img loading="lazy" decoding="async" src="assets/images/gallery/WhatsApp Image 2026-08-07 at 1.39.48 PM (1).jpeg" alt="Franciscan Ashram Kashiabari" style="width: 100%; height: 200px; object-fit: cover;">
                <div style="padding: 1.5rem;">
                    <h4 style="font-family: 'Phudu', sans-serif; font-size: 1.1rem; font-weight: 700; color: #1c1917; margin-top: 0; margin-bottom: 0.5rem;">Franciscan Ashram Kashiabari</h4>
                    <p style="font-family: 'Instrument Sans', sans-serif; font-size: 0.95rem; color: #57534e; line-height: 1.5; margin: 0;">Village Sindrijhora, P.O. Kashiabari Kokrajhar, ASSAM - 783360</p>
                </div>
            </div>
        </div>"""

new_bongaigaon = """        <h3 style="font-family: 'Phudu', sans-serif; font-size: 1.5rem; font-weight: 700; color: #4a2a18; margin-top: 3rem; margin-bottom: 1.5rem; border-bottom: 2px solid #e5e5e5; padding-bottom: 0.5rem;">DIOCESE OF BONGAIGAON</h3>
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 580px)); gap: 2rem; margin-bottom: 2rem;">
            <div style="border-radius: 12px; overflow: hidden; box-shadow: 0 10px 20px rgba(0,0,0,0.05); display: flex; flex-direction: column; background: #fff;">
                <img loading="lazy" decoding="async" src="assets/images/gallery/WhatsApp Image 2026-08-07 at 1.39.48 PM (1).jpeg" alt="Franciscan Ashram Kashiabari" style="width: 100%; height: 200px; object-fit: cover;">
                <div style="padding: 1.5rem;">
                    <h4 style="font-family: 'Phudu', sans-serif; font-size: 1.1rem; font-weight: 700; color: #1c1917; margin-top: 0; margin-bottom: 0.5rem;">Franciscan Ashram Kashiabari</h4>
                    <p style="font-family: 'Instrument Sans', sans-serif; font-size: 0.95rem; color: #57534e; line-height: 1.5; margin: 0;">Village Sindrijhora, P.O. Kashiabari Kokrajhar, ASSAM - 783360</p>
                </div>
            </div>
        </div>"""

c = c.replace(old_bongaigaon, new_bongaigaon)

with open(friaries_html, 'w', encoding='utf-8') as f:
    f.write(c)
print("Updated community-friaries.html layout successfully!")

# 2. Add favicon to all HTML files
html_files = [f for f in os.listdir('.') if f.endswith('.html')]
favicon_tag = '    <link rel="icon" type="image/svg+xml" href="assets/images/logo.svg">\n    <link rel="shortcut icon" href="assets/images/logo.svg">\n'

for fname in html_files:
    with open(fname, 'r', encoding='utf-8') as f:
        content = f.read()

    if 'rel="icon"' not in content and '<head>' in content:
        content = content.replace('<head>', '<head>\n' + favicon_tag, 1)
        with open(fname, 'w', encoding='utf-8') as f:
            f.write(content)

print(f"Added favicon tags across all {len(html_files)} HTML files.")
