import os
from bs4 import BeautifulSoup

def convert_ssl_plans():
    file_path = r'd:\DN\NIKITA_RAjput\ssl.html'
    
    with open(file_path, 'r', encoding='utf-8') as f:
        html_content = f.read()
        
    soup = BeautifulSoup(html_content, 'html.parser')
    
    # The custom tables contain the cards
    custom_tables = soup.find_all('div', class_='custom-table')
    
    for table in custom_tables:
        cards = table.find_all('div', class_='card', recursive=False)
        for card in cards:
            # Create a new plan container
            plan_div = soup.new_tag('div', attrs={'class': 'plan', 'style': 'height: 100%; display: flex; flex-direction: column;'})
            
            # --- Plan Head ---
            plan_head = soup.new_tag('div', attrs={'class': 'plan-head'})
            
            # Extract image
            img_wrap = card.find('div', class_='ssl-image-wrap')
            if img_wrap:
                # To match cloud's aesthetic, we can put the image in the head
                img_wrap['style'] = "text-align: center; margin-bottom: 15px;"
                img = img_wrap.find('img')
                if img:
                    img['style'] = "width: 48px; height: 48px; object-fit: contain;"
                plan_head.append(img_wrap)
                
            # Extract title
            title = card.find('h2', class_='card-title')
            if title:
                plan_name = soup.new_tag('h3', attrs={'class': 'plan-name'})
                plan_name.string = title.get_text(strip=True)
                plan_head.append(plan_name)
                
            # Extract "MOST POPULAR" tag if exists
            popular_tag = card.find('div', class_='tag-most-plpr')
            if popular_tag:
                tag_p = soup.new_tag('p', attrs={'class': 'plan-para', 'style': 'color: #0d6efd; font-weight: bold; font-size: 13px;'})
                tag_p.string = popular_tag.get_text(strip=True)
                plan_head.append(tag_p)
                
            plan_div.append(plan_head)
            
            # --- Plan Price ---
            plan_price = soup.new_tag('div', attrs={'class': 'plan-price'})
            pricing = card.find('div', class_='pricing')
            if pricing:
                h4_price = soup.new_tag('h4', attrs={'class': 'price'})
                # We need to extract the price text and the span text
                # The pricing div looks like: <div class="pricing"><img...> ₹723.94<span class="period">/yr</span></div>
                span_period = pricing.find('span', class_='period')
                
                # Extract text excluding the span
                price_text = ''
                for content in pricing.contents:
                    if isinstance(content, str) and content.strip():
                        price_text += content.strip()
                        
                from_span = pricing.find('span', class_='card-price-span')
                if from_span:
                    price_text = from_span.get_text(strip=True) + " " + price_text
                
                if span_period:
                    span_tag = soup.new_tag('span')
                    span_tag.string = span_period.get_text(strip=True)
                    h4_price.string = price_text
                    h4_price.append(span_tag)
                else:
                    h4_price.string = price_text
                    
                plan_price.append(h4_price)
                
            plan_div.append(plan_price)
            
            # --- Plan Body ---
            plan_body = soup.new_tag('div', attrs={'class': 'plan-body', 'style': 'flex-grow: 1; display: flex; flex-direction: column;'})
            ul = soup.new_tag('ul', attrs={'class': 'list list-unstyled', 'style': 'flex-grow: 1;'})
            
            # Extract features
            card_body = card.find('div', class_='card-body')
            if card_body:
                points = card_body.find_all('div', class_='card-trust-point')
                for point in points:
                    li = soup.new_tag('li')
                    
                    icon_img = soup.new_tag('img', attrs={'src': 'assetst/images/pages/shared-hosting/check-circle.svg', 'class': 'img-fluid', 'alt': 'icon'})
                    li.append(icon_img)
                    
                    div_text = soup.new_tag('div')
                    strong_text = soup.new_tag('strong', attrs={'style': 'display:block;'})
                    
                    # Remove the check-icon span
                    check_icon = point.find('span', class_='check-icon')
                    if check_icon:
                        check_icon.extract()
                        
                    strong_text.string = point.get_text(strip=True)
                    div_text.append(strong_text)
                    li.append(div_text)
                    
                    ul.append(li)
                    
            plan_body.append(ul)
            
            # Extract Footer (Button)
            card_footer = card.find('div', class_='card-footer')
            if card_footer:
                actions = soup.new_tag('div', attrs={'class': 'actions', 'style': 'margin-top: 20px;'})
                btn = card_footer.find('button', class_='buy-btn')
                if btn:
                    new_btn = soup.new_tag('a', attrs={'href': '#', 'class': 'btn btn-fill-primary shadow-off w-100'})
                    new_btn.string = btn.get_text(strip=True)
                    actions.append(new_btn)
                elif card_footer.find('a'):
                    old_a = card_footer.find('a')
                    old_a['class'] = 'btn btn-fill-primary shadow-off w-100'
                    actions.append(old_a)
                plan_body.append(actions)
                
            plan_div.append(plan_body)
            
            # Replace the old card with the new plan_div
            card.replace_with(plan_div)

    # Change custom-table to use bootstrap grid so it mimics cloud.html exactly
    for table in custom_tables:
        table['class'] = table.get('class', []) + ['row']
        table['style'] = table.get('style', '') + '; display: flex; flex-wrap: wrap; margin-left: -15px; margin-right: -15px;'
        
        # wrap each plan in a col-xl-3 col-md-6 mb-4
        plans = table.find_all('div', class_='plan', recursive=False)
        for plan in plans:
            col = soup.new_tag('div', attrs={'class': 'col-xl-4 col-md-6 mb-4 px-3'})
            plan.wrap(col)
            
        # We should also adjust .custom-table to NOT act like a css grid if we added row.
        # But we added inline style display: flex; flex-wrap: wrap to override it.

    # Write back
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(str(soup))
        
    print("Conversion completed successfully.")

if __name__ == "__main__":
    convert_ssl_plans()
