#!/usr/bin/env python3
"""
Script to update services listing pages with services from scraped content.
"""

import re

# Services from scraped content
ENGLISH_SERVICES = [
    "Design and Build",
    "Infrastructure Projects",
    "Steel Building Projects",
    "Civil Works",
    "Electrical Projects",
    "Mechanical Projects",
    "Process Engineering & Execution",
    "Plant Machinery Engineering & Installation",
    "Special Metal Fabrication",
    "Project Management"
]

ARABIC_SERVICES = [
    "التصميم والبناء",
    "مشاريع البنية التحتية",
    "مشاريع المباني الفولاذية",
    "الأعمال المدنية",
    "المشاريع الكهربائية",
    "المشاريع الميكانيكية",
    "هندسة العمليات والتنفيذ",
    "هندسة وتثبيت آلات المصانع",
    "تصنيع المعادن الخاصة",
    "إدارة المشاريع"
]

# Service descriptions in English
ENGLISH_DESCRIPTIONS = {
    "Design and Build": "Our integrated Design and Build service combines architectural design, engineering, and construction expertise to deliver turnkey solutions. We manage the entire project lifecycle from concept to completion, ensuring seamless coordination and optimal results.",
    "Infrastructure Projects": "We specialize in developing robust infrastructure including roads, bridges, utilities, and transportation systems. Our expertise covers planning, design, and construction of critical infrastructure that supports economic growth and community development.",
    "Steel Building Projects": "We deliver high-quality steel structure projects including warehouses, industrial facilities, and commercial buildings. Our steel construction expertise ensures durable, cost-effective, and rapidly deployable building solutions.",
    "Civil Works": "Our civil works services encompass site preparation, earthworks, foundations, and structural concrete works. We provide comprehensive civil engineering solutions for residential, commercial, and industrial projects.",
    "Electrical Projects": "We handle complete electrical systems installation including power distribution, lighting, control systems, and renewable energy integration. Our electrical projects meet international standards and ensure reliable, efficient power solutions.",
    "Mechanical Projects": "Our mechanical engineering services cover HVAC systems, plumbing, fire protection, and industrial machinery installation. We deliver energy-efficient mechanical solutions that enhance building performance and occupant comfort.",
    "Process Engineering & Execution": "We provide process engineering services for industrial facilities, including process design, optimization, and execution. Our expertise ensures efficient production processes and operational excellence.",
    "Plant Machinery Engineering & Installation": "We specialize in the engineering, procurement, and installation of industrial plant machinery and equipment. Our services ensure proper integration and optimal performance of manufacturing systems.",
    "Special Metal Fabrication": "Our metal fabrication services produce custom steel and metal components for construction and industrial applications. We deliver precision-engineered metalwork including structural elements, equipment, and specialized components.",
    "Project Management": "Our project management services ensure successful project delivery through comprehensive planning, coordination, and execution. We manage timelines, budgets, resources, and quality to achieve project objectives efficiently."
}

# Service descriptions in Arabic
ARABIC_DESCRIPTIONS = {
    "التصميم والبناء": "تجمع خدماتنا المتكاملة للتصميم والبناء بين التصميم المعماري والهندسة وخبرة البناء لتقديم حلول جاهزة. ندير دورة حياة المشروع بالكامل من المفهوم إلى الإنجاز، مما يضمن التنسيق السلس والنتائج المثلى.",
    "مشاريع البنية التحتية": "نتخصص في تطوير البنية التحتية القوية بما في ذلك الطرق والجسور والمرافق وأنظمة النقل. تغطي خبرتنا التخطيط والتصميم والبناء للبنية التحتية الحرجة التي تدعم النمو الاقتصادي وتنمية المجتمع.",
    "مشاريع المباني الفولاذية": "نقدم مشاريع هياكل فولاذية عالية الجودة بما في ذلك المستودعات والمرافق الصناعية والمباني التجارية. تضمن خبرتنا في البناء الفولاذي حلول بناء متينة وفعالة من حيث التكلفة وقابلة للنشر بسرعة.",
    "الأعمال المدنية": "تشمل خدماتنا للأعمال المدنية إعداد الموقع والأعمال الترابية والأساسات وأعمال الخرسانة الهيكلية. نقدم حلول هندسية مدنية شاملة للمشاريع السكنية والتجارية والصناعية.",
    "المشاريع الكهربائية": "نتعامل مع تركيب أنظمة كهربائية كاملة بما في ذلك توزيع الطاقة والإضاءة وأنظمة التحكم وتكامل الطاقة المتجددة. تلبي مشاريعنا الكهربائية المعايير الدولية وتضمن حلول طاقة موثوقة وفعالة.",
    "المشاريع الميكانيكية": "تغطي خدماتنا الهندسية الميكانيكية أنظمة التدفئة والتهوية وتكييف الهواء والسباكة وحماية الحرائق وتركيب الآلات الصناعية. نقدم حلول ميكانيكية موفرة للطاقة تعزز أداء المبنى وراحة السكان.",
    "هندسة العمليات والتنفيذ": "نقدم خدمات هندسة العمليات للمرافق الصناعية، بما في ذلك تصميم العمليات والتحسين والتنفيذ. تضمن خبرتنا عمليات إنتاج فعالة والتميز التشغيلي.",
    "هندسة وتثبيت آلات المصانع": "نتخصص في الهندسة والمشتريات وتركيب آلات ومعدات المصانع الصناعية. تضمن خدماتنا التكامل السليم والأداء الأمثل لأنظمة التصنيع.",
    "تصنيع المعادن الخاصة": "تنتج خدماتنا لتصنيع المعادن مكونات فولاذية ومعدنية مخصصة لتطبيقات البناء والصناعية. نقدم أعمال معدنية مصممة بدقة بما في ذلك العناصر الهيكلية والمعدات والمكونات المتخصصة.",
    "إدارة المشاريع": "تضمن خدماتنا لإدارة المشاريع نجاح تسليم المشروع من خلال التخطيط الشامل والتنسيق والتنفيذ. ندير الجداول الزمنية والميزانيات والموارد والجودة لتحقيق أهداف المشروع بكفاءة."
}

# Service box template for English
ENGLISH_SERVICE_BOX = '''<div class="service-box">
<img alt="" src="./assets/scraped_images/WhatsApp-Image-2022-10-22-at-5.20.12-PM-1-600x300.jpeg"/>
<div class="service-box-content">
<div class="service-box--img">
<img src="./assets/icon/business-and-trade copy 3.png"/>
</div>
<p>{service_name}</p>
</div>
<div class="hoveredContent">
<div class="d-flex align-items-center hoveredTitle">
<div class="hoveredIcon">
<img src="./assets/icon/business-and-trade copy 3.png"/>
</div>
<p>{service_name}</p>
</div>
<div class="hoveredContent--bottom">
<p>
                  {service_description}
                </p>
<a href="./ourServicedetail.html">
<button>
<svg class="lucide lucide-arrow-right-circle" fill="none" height="24" stroke="#4A90E2" 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> Read more </span>
</button>
</a>
</div>
</div>
<div class="bg-overlay-black"></div>
</div>'''

# Service box template for Arabic
ARABIC_SERVICE_BOX = '''<div class="service-box">
<img alt="" src="./assets/scraped_images/WhatsApp-Image-2022-10-22-at-5.20.12-PM-1-600x300.jpeg"/>
<div class="service-box-content">
<div class="service-box--img">
<img src="./assets/icon/business-and-trade copy 3.png"/>
</div>
<p>{service_name}</p>
</div>
<div class="hoveredContent">
<div class="d-flex align-items-center hoveredTitle">
<div class="hoveredIcon">
<img src="./assets/icon/business-and-trade copy 3.png"/>
</div>
<p>{service_name}</p>
</div>
<div class="hoveredContent--bottom">
<p>
                  {service_description}
                </p>
<a href="./ourServicedetail-ar.html">
<button>
<svg class="lucide lucide-arrow-right-circle" fill="none" height="24" stroke="#4A90E2" 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>
</a>
</div>
</div>
<div class="bg-overlay-black"></div>
</div>'''


def update_services_page(file_path, is_arabic=False):
    """Update the services listing page with services from scraped content."""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
    except Exception as e:
        print(f"Error reading {file_path}: {e}")
        return False
    
    # Find the services container
    # Look for the container div that holds all service boxes
    container_pattern = r'(<div class="container digital-engineering-ourServices">)(.*?)(</div>\s*</section>)'
    
    match = re.search(container_pattern, content, re.DOTALL)
    if not match:
        print(f"Could not find services container in {file_path}")
        return False
    
    container_start = match.start(1)
    container_end = match.end(3)
    
    # Generate service boxes
    services = ARABIC_SERVICES if is_arabic else ENGLISH_SERVICES
    template = ARABIC_SERVICE_BOX if is_arabic else ENGLISH_SERVICE_BOX
    descriptions = ARABIC_DESCRIPTIONS if is_arabic else ENGLISH_DESCRIPTIONS
    
    service_boxes_html = '\n'.join([
        template.format(
            service_name=service,
            service_description=descriptions.get(service, "")
        ) for service in services
    ])
    
    # Replace the content
    new_content = content[:container_start] + match.group(1) + '\n' + service_boxes_html + '\n' + match.group(3) + content[container_end:]
    
    # Write back
    try:
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(new_content)
        print(f"✓ Updated {file_path}")
        return True
    except Exception as e:
        print(f"Error writing {file_path}: {e}")
        return False


def main():
    """Main function."""
    base_dir = '/media/tpct/main/shiftcodes/smc01sc2025.shiftcodes.net'
    
    # Update English services page
    english_file = f'{base_dir}/ourService.html'
    update_services_page(english_file, is_arabic=False)
    
    # Update Arabic services page
    arabic_file = f'{base_dir}/ourService-ar.html'
    update_services_page(arabic_file, is_arabic=True)
    
    print("\nDone!")


if __name__ == '__main__':
    main()
