import os
import re

css_vine_rule = """
/* ============================================================
   REUSABLE DESIGN PATTERN: vine-corner-watermark
   ============================================================ */
.has-vine-watermark {
    position: relative !important;
    overflow: hidden !important;
}

.vine-corner-watermark {
    position: absolute !important;
    top: 0 !important;
    right: 0 !important;
    width: clamp(200px, 24vw, 380px) !important;
    height: clamp(220px, 26vw, 420px) !important;
    background-image: url('assets/images/shapes/vine-corner-watermark.png') !important;
    background-repeat: no-repeat !important;
    background-position: top right !important;
    background-size: contain !important;
    pointer-events: none !important;
    opacity: 0.22 !important;
    z-index: 1 !important;
}
"""

# 1. Update css/styles.css
with open(r'css\styles.css', 'r', encoding='utf-8') as f:
    c = f.read()
c += "\n" + css_vine_rule
with open(r'css\styles.css', 'w', encoding='utf-8') as f:
    f.write(c)

# 2. Update css/design-system.css
with open(r'css\design-system.css', 'r', encoding='utf-8') as f:
    c = f.read()
c += "\n" + css_vine_rule
with open(r'css\design-system.css', 'w', encoding='utf-8') as f:
    f.write(c)

# 3. Add to index.html news section
with open('index.html', 'r', encoding='utf-8') as f:
    index_c = f.read()

index_c = index_c.replace(
    '<section id="news-section" style="',
    '<section id="news-section" class="has-vine-watermark" style="position: relative; '
)
index_c = index_c.replace(
    '<section id="news-section" class="has-vine-watermark" style="position: relative; padding: clamp(2rem, 4vw, 3.5rem) 0; background-color: #F5F3EC; color: #1c1917; box-sizing: border-box; overflow: hidden;">',
    '<section id="news-section" class="has-vine-watermark" style="position: relative; padding: clamp(2rem, 4vw, 3.5rem) 0; background-color: #F5F3EC; color: #1c1917; box-sizing: border-box; overflow: hidden;">\n            <div class="vine-corner-watermark" aria-hidden="true"></div>'
)
with open('index.html', 'w', encoding='utf-8') as f:
    f.write(index_c)

# 4. Add to news.html
if os.path.exists('news.html'):
    with open('news.html', 'r', encoding='utf-8') as f:
        news_c = f.read()
    news_c = re.sub(
        r'(<main[^>]*>.*?</section>\s*<section[^>]*style="[^"]*background-color:\s*#FFFFFF[^"]*")',
        r'\1 class="has-vine-watermark">\n            <div class="vine-corner-watermark" aria-hidden="true"></div',
        news_c,
        flags=re.DOTALL
    )
    with open('news.html', 'w', encoding='utf-8') as f:
        f.write(news_c)

# 5. Add to publications.html
if os.path.exists('publications.html'):
    with open('publications.html', 'r', encoding='utf-8') as f:
        pub_c = f.read()
    pub_c = pub_c.replace(
        '<section style="padding: clamp(3.5rem, 6vw, 5.5rem) 0; background-color: #FFFFFF; color: #1c1917; min-height: 500px;">',
        '<section class="has-vine-watermark" style="position: relative; padding: clamp(3.5rem, 6vw, 5.5rem) 0; background-color: #FFFFFF; color: #1c1917; min-height: 500px; overflow: hidden;">\n            <div class="vine-corner-watermark" aria-hidden="true"></div>'
    )
    with open('publications.html', 'w', encoding='utf-8') as f:
        f.write(pub_c)

print("Applied vine-corner-watermark to News and Publications sections successfully!")
