import re
import sys

# 1. Read final-page.html to get Header, Hero, and Pricing base
with open(r'd:\DN\final_page_project\final-page.html', 'r', encoding='utf-8') as f:
    final_html = f.read()

# Extract up to the end of pricing section
# The pricing section ends where? Let's find the end of the pricing section container.
# In final-page.html, the pricing section is: <section id="pricing" class="pricing-integration"> ... </section>
match_pricing = re.search(r'(<section id="pricing".*?</section>)', final_html, re.DOTALL)
if not match_pricing:
    print("Could not find pricing section in final-page.html")
    sys.exit(1)

pricing_html = match_pricing.group(1)

# Extract everything before pricing
before_pricing = final_html[:match_pricing.start()]

# Apply Hero replacements
before_pricing = before_pricing.replace('<title>CloudMate - One Stop for All Your Business Needs</title>', '<title>Shared Hosting - CloudMate</title>')
before_pricing = before_pricing.replace('<h1>Cloud Hosting <br />Built for Your Success</h1>', '<h1>Building your own<br>website just got easier.</h1>')
before_pricing = before_pricing.replace(
    '<p>\n                        Fast, secure, and reliable cloud hosting services powered by\n                        enterprise-grade infrastructure. Perfect for businesses of all\n                        sizes.\n                    </p>',
    '<p>CloudMate Shared Hosting services delivers a Low-cost affordable hosting to get you started, perfect for your blogs/business websites.</p>'
)
lottie_html = '''<script src="https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js"></script>
                    <lottie-player src="https://assets8.lottiefiles.com/packages/lf20_tnoqsv4p.json" background="transparent" speed="1" style="width: 100%; height: auto; max-width: 650px; margin: auto;" loop autoplay></lottie-player>'''
before_pricing = before_pricing.replace('<img src="assets/banner-image.png" alt="Cloud Hosting Illustration" />', lottie_html)

# Apply Pricing replacements
pricing_html = pricing_html.replace('<h4 class="se-title-2">Get More Power With Our Managed Business Plans</h4>', '<h4 class="se-title-2">Shared server hosting plans and pricing</h4>')

with open(r'C:\Users\Nikita Rajput\.gemini\antigravity-ide\brain\28413561-d1e4-432f-92a9-82b86e63153c\.system_generated\steps\42\content.md', 'r', encoding='utf-8') as f:
    shared_md = f.read()
match_md = re.search(r'<div class="row">\s*<!-- col -->\s*<div class="col-xl-3(.*?)</div>\s*</div>\s*</div>\s*<!-- se-footer -->', shared_md, re.DOTALL)
if match_md:
    plans_html = '<div class="row mx-xl-0">\n          <!-- col -->\n          <div class="col-xl-3' + match_md.group(1) + '</div>\n          </div>'
    plans_html = plans_html.replace('data-src="', 'src="')
    plans_html = plans_html.replace('src="/assets/', 'src="assets/')
    pricing_html = re.sub(r'<div class="row mx-xl-0">.*?</div>\s*</div>\s*<!-- se-footer -->', plans_html + '\n        </div>\n        <!-- se-footer -->', pricing_html, flags=re.DOTALL)

# 2. Read shared_hosting.html to get FAQ and Footer
with open(r'd:\DN\shared\shared_hosting.html', 'r', encoding='utf-8') as f:
    shared_html = f.read()

# Extract FAQ
faq_match = re.search(r'(<!-- FAQs -->.*?</div>\s*</div>\s*</div>)', shared_html, re.DOTALL)
faq_html = faq_match.group(1) if faq_match else ""

# Extract Footer (Theme Footer and below)
footer_match = re.search(r'(<!-- Theme Footer -->.*</html>)', shared_html, re.DOTALL)
footer_html = footer_match.group(1) if footer_match else ""

# Wait, if we use the footer from shared_hosting.html, it includes the closing </body></html>.
# What about the scripts from final-page.html (like the chat popup)? 
# If the user said "fir about section", they might mean the footer of shared_hosting.html.

# Let's assemble
final_output = before_pricing + pricing_html + "\n\n" + faq_html + "\n\n" + footer_html

with open(r'd:\DN\final_page_project\shared-hosting.html', 'w', encoding='utf-8') as f:
    f.write(final_output)

print("Cleaned up shared-hosting.html successfully!")
