import re

# The 6 actual news titles from news.html
news_titles = [
    "STRENGTHENING YOUR FAITH THROUGH CONSISTENT DAILY PRAYER LIFE",
    "TRUSTING GOD FULLY DURING LIFE'S DIFFICULT TIMES",
    "STAYING SPIRITUALLY STRONG THROUGH FAITH",
    "EXPANDING FRANCISCAN EDUCATIONAL MISSIONS",
    "YOUTH SPIRITUAL RETREAT & FELLOWSHIP",
    "ST. FRANCIS PROVINCIA FEAST DAY"
]

# Placeholder titles for the blog items in index.html
blog_titles = [
    "Exploring the Franciscan Way",
    "Finding Peace in Daily Life",
    "The Spirit of Community Service",
    "Reflections on Morning Prayer",
    "Bringing Hope to the Margins",
    "A Journey of Faith and Fellowship"
]

# The original blog titles in index.html that need to be replaced
old_blog_titles = [
    "CHILDREN'S MINISTRY",
    "YOUTH MINISTRY",
    "WOMEN'S MINISTRY",
    "PRAYER &amp; INTERCESSION",  # taking into account HTML entities
    "LITURGY &amp; WORSHIP",
    "OUTREACH &amp; CHARITY"
]
old_blog_titles_text = [
    "CHILDREN'S MINISTRY",
    "YOUTH MINISTRY",
    "WOMEN'S MINISTRY",
    "PRAYER & INTERCESSION",
    "LITURGY & WORSHIP",
    "OUTREACH & CHARITY"
]

def title_case(s):
    # Basic title case for breadcrumbs
    return " ".join([word.capitalize() for word in s.split()])

def generate_pages():
    with open('news-detail.html', 'r', encoding='utf-8') as f:
        template = f.read()

    # Generate News Detail Pages
    for i, title in enumerate(news_titles):
        new_content = re.sub(
            r'<h1 class="article-title" id="article-title">.*?</h1>',
            f'<h1 class="article-title" id="article-title">{title}</h1>',
            template,
            flags=re.DOTALL
        )
        new_content = re.sub(
            r'<span id="breadcrumb-title">.*?</span>',
            f'<span id="breadcrumb-title">{title_case(title)}</span>',
            new_content,
            flags=re.DOTALL
        )
        new_content = re.sub(
            r'<title>.*?</title>',
            f'<title>{title_case(title)} | The Franciscan Society</title>',
            new_content,
            flags=re.DOTALL
        )
        with open(f'news-detail-{i+1}.html', 'w', encoding='utf-8') as f:
            f.write(new_content)
        print(f"Created news-detail-{i+1}.html")

    # Generate Blog Detail Pages
    for i, title in enumerate(blog_titles):
        new_content = re.sub(
            r'<h1 class="article-title" id="article-title">.*?</h1>',
            f'<h1 class="article-title" id="article-title">{title.upper()}</h1>',
            template,
            flags=re.DOTALL
        )
        new_content = re.sub(
            r'<span id="breadcrumb-title">.*?</span>',
            f'<span id="breadcrumb-title">{title}</span>',
            new_content,
            flags=re.DOTALL
        )
        new_content = re.sub(
            r'<title>.*?</title>',
            f'<title>{title} | The Franciscan Society</title>',
            new_content,
            flags=re.DOTALL
        )
        # Change Breadcrumb "News" to "Blog"
        new_content = new_content.replace('>News</a>', '>Blog</a>')
        with open(f'blog-detail-{i+1}.html', 'w', encoding='utf-8') as f:
            f.write(new_content)
        print(f"Created blog-detail-{i+1}.html")

def update_links():
    # Update news.html
    with open('news.html', 'r', encoding='utf-8') as f:
        news_content = f.read()
    
    for i in range(6):
        news_content = news_content.replace(f'news-detail.html?id={i+1}', f'news-detail-{i+1}.html')
    
    with open('news.html', 'w', encoding='utf-8') as f:
        f.write(news_content)
    print("Updated news.html links")

    # Update index.html
    with open('index.html', 'r', encoding='utf-8') as f:
        index_content = f.read()

    # In index.html, the first 3 links are news 1, 2, 3. The next 6 are blog 1-6 but they currently point to ?id=3
    # Let's do this carefully.
    
    # 1. Update the 6 blog cards
    # We will search for each old blog title and replace it and the text below it.
    for i, old_title in enumerate(old_blog_titles):
        # The title in h3 might have uppercase
        # We need to replace the h3 content
        index_content = re.sub(
            rf'<h3[^>]*>\s*{old_title}\s*</h3>',
            f'<h3 style="font-family: \'Phudu\', sans-serif !important; font-size: 1.25rem !important; font-weight: 600 !important; color: #1c1917 !important; text-transform: uppercase; margin-bottom: 0.8rem; letter-spacing: 0.01em; line-height: 1.3;">\n                            {blog_titles[i].upper()}\n                        </h3>',
            index_content,
            flags=re.IGNORECASE
        )
        
        # The paragraph text
        old_text_prefix = old_blog_titles_text[i] + " HOME MEETING"
        new_text = f"An inspiring reflection on {blog_titles[i].lower()}."
        index_content = re.sub(
            rf'<p[^>]*>\s*{old_text_prefix}.*?</p>',
            f'<p style="font-family: \'Instrument Sans\', sans-serif !important; font-size: 0.88rem; color: #57534e; line-height: 1.6; margin-bottom: 1.6rem;">\n                            {new_text}\n                        </p>',
            index_content,
            flags=re.IGNORECASE | re.DOTALL
        )

    # 2. Update links in index.html
    # Since the blog links all point to news-detail.html?id=3, it's hard to distinguish which is which simply by string replace.
    # We can split the file by blog-padded-card to replace the hrefs in order.
    parts = index_content.split('class="blog-padded-card"')
    if len(parts) == 7: # 1 preamble + 6 cards
        for i in range(1, 7):
            parts[i] = re.sub(r'href="news-detail\.html[^"]*"', f'href="blog-detail-{i}.html"', parts[i], count=1)
        index_content = 'class="blog-padded-card"'.join(parts)
        print("Updated blog links in index.html")
    else:
        print(f"Warning: Expected 6 blog cards, found {len(parts)-1}")

    # For the news section in index.html, it has links to ?id=1, ?id=2, ?id=3
    for i in range(3):
        index_content = index_content.replace(f'news-detail.html?id={i+1}', f'news-detail-{i+1}.html')

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

if __name__ == '__main__':
    generate_pages()
    update_links()
