#!/usr/bin/env python3
"""
Fix the Arabic service detail page structure to match the English version.
"""

import re

def fix_arabic_service_detail():
    file_path = 'ourServicedetail-ar.html'
    
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # Fix the broken structure - replace the malformed section
    # Find the section starting at digital-engineering and fix it
    pattern = r'(<section id="digital-engineering">.*?<div class="container">.*?<p class="paragraph-1">.*?</p>)(.*?)(<div class="service-box">)'
    
    replacement = r'''\1
          <p class="paragraph-2">
            نحن ملتزمون ببناء مساحات سكنية عالية الجودة تعزز حياة الناس. تشمل مشاريعنا السكنية مجموعة متنوعة من خيارات السكن، من المنازل الفردية والشقق إلى المنازل المتلاصقة والمجمعات السكنية.
          </p>
        </div>
      </section>
      <section class="digital-engineering-slider">
        <div class="container gridImage-Gallery">
          <div class="figure">
            <img
              src="assets/scraped_images/1-1-1-e1723133568557-600x300.jpg"
            />
          </div>

          <div class="thumbnails">
            <div><img class="active" src="assets/scraped_images/1-1-1-e1723133568557-600x300.jpg"/></div>
            <div><img class="" src="assets/scraped_images/1-1-600x300.jpeg"/></div>
            <div><img class="" src="assets/scraped_images/1-16-1.jpg"/></div>
            <div><img class="" src="assets/scraped_images/1-17-1-600x300.jpg"/></div>
          </div>
        </div>
        <div class="container"></div>
      </section>
      <section class="digital-engineering--services container">
        <h5>خدماتنا في الهندسة الرقمية تمكنك من:</h5>
        <div class="digital-engineering--bullets">
          <p>التخطيط والإدارة الشاملة للمشاريع</p>
          <p>خدمات الهندسة والتصميم المتخصصة</p>
          <p>البناء والتنفيذ عالي الجودة</p>
          <p>تسليم المشاريع في الوقت المحدد</p>
          <p>الالتزام بالسلامة والامتثال</p>
        </div>
        <p class="digital-engineering-text">
          من خلال خدماتنا الشاملة للبناء، نجمع بين الخبرات الواسعة والقدرات لتقديم نتائج استثنائية لعملائنا. التزامنا بالجودة والسلامة والتسليم في الوقت المحدد يميزنا في الصناعة.
        </p>
      </section>

      <section class="sectionContainer">
        <div class="sectionContainer--center">
          <h5>مشاريع أكبر وأفضل وأسرع وأقوى.</h5>
          <h3>خدماتنا</h3>
        </div>

        <div class="container digital-engineering-ourServices">
          \3'''
    
    content = re.sub(pattern, replacement, content, flags=re.DOTALL)
    
    # Fix the footer
    footer_pattern = r'(</main>\s*<!-- footer goes here  -->\s*<footer></footer>)'
    footer_replacement = '''</main>

    <!-- Footer -->
    <footer class="footer" id="footer">
      <img alt="" class="footerPattern" src="assets/png.png"/>
      <div class="container footerContainer">
        <div class="footerLinks--container">
          <div class="footerLinks">
            <h3>روابط سريعة</h3>
            <a href="home-ar.html">الرئيسية</a>
            <a href="about-ar.html">من نحن</a>
            <a href="ourService-ar.html">الخدمات</a>
            <a href="project-ar.html">المشاريع</a>
            <a href="Latestnews-ar.html">آخر الأخبار</a>
          </div>
          <div class="footerLinks FooterPaddingTop">
            <a href="join-ar.html">انضم إلينا</a>
            <a href="contactUs-ar.html">اتصل بنا</a>
            <a href="PrivacyPolicy-ar.html">سياسة الخصوصية</a>
            <a href="search-ar.html">بحث</a>
          </div>
          <div class="footerLinks FooterPaddingTop">
            <a href="about-ar.html#certificates">الشهادات</a>
            <a href="about-ar.html#clients">العملاء</a>
          </div>
          <div class="footerLinks footerLinksForm--container">
            <h3>النشرة الإخبارية</h3>
            <form class="d-flex flex-column gap-3">
              <input class="p-3" placeholder="info@example.com" required="" type="email"/>
              <button class="footer-subscribe-btn" type="submit">
                <svg class="lucide lucide-arrow-right-circle" fill="none" height="24" stroke="#fff" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
                  <circle cx="12" cy="12" r="10"></circle>
                  <path d="M8 12h8"></path>
                  <path d="m12 16 4-4-4-4"></path>
                </svg>
                <span>اشترك</span>
              </button>
            </form>
          </div>
        </div>
      </div>
    </footer>'''
    
    content = re.sub(footer_pattern, footer_replacement, content, flags=re.DOTALL)
    
    # Remove orphaned divs before service-box
    content = re.sub(r'(\s*<div class="hoveredContent--bottom">.*?</div>\s*</div>\s*<div class="bg-overlay-black"></div>\s*</div>\s*)(<div class="service-box">)', r'\2', content, flags=re.DOTALL)
    
    # Fix closing tags before </main>
    content = re.sub(r'(\s*</div>\s*)(</main>)', r'\1        </div>\n      </section>\n    \2', content)
    
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(content)
    
    print(f"✓ Fixed {file_path}")

if __name__ == '__main__':
    fix_arabic_service_detail()
