import urllib.request
from bs4 import BeautifulSoup
import re
import gzip
import os
import time
import sys

os.makedirs('resources/views/blog-posts', exist_ok=True)

with open('blog_slugs.txt', 'r', encoding='utf-8') as f:
    slugs = [line.strip().lstrip('/') for line in f if line.strip()]

def make_spa(text):
    text = re.sub(r'<a([^>]+)href=["\']https://sarabeauty\.ae/([^"\']*)["\']', r'<a\1href="/\2" wire:navigate', text)
    text = re.sub(r'<a([^>]+)href=["\']https://sarabeauty\.ae["\']', r'<a\1href="/" wire:navigate', text)
    text = re.sub(r'(href|src|srcset|data-lazy-src)=["\']/(wp-content|wp-includes|wp-json)([^"\']*)["\']', r'\1="https://sarabeauty.ae/\2\3"', text)
    text = re.sub(r'url\([\'"]?/(wp-content|wp-includes)([^)\'"]*)[\'"]?\)', r'url(https://sarabeauty.ae/\1\2)', text)
    text = text.replace('"@', '"@@')
    return text

def scrape_article(slug):
    out_path = f'resources/views/blog-posts/{slug}.blade.php'
    if os.path.exists(out_path):
        print(f"Already scraped: {slug}")
        return True

    url = f"https://sarabeauty.ae/{slug}/"
    print(f"Fetching {url}...")
    sys.stdout.flush()
    try:
        req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0', 'Accept-Encoding': 'identity'})
        with urllib.request.urlopen(req, timeout=20) as response:
            data = response.read()
            if response.info().get('Content-Encoding') == 'gzip':
                data = gzip.decompress(data)
            html = data.decode('utf-8')
    except Exception as e:
        print(f"Error fetching {slug}: {e}")
        sys.stdout.flush()
        return False

    soup = BeautifulSoup(html, 'html.parser')

    # IMPORTANT: check 'single-post'/'wp-page' BEFORE 'wp-post' - the header/
    # footer template widgets are also marked data-elementor-type="wp-post"
    # (post-type elementskit_template), and BeautifulSoup .find() returns the
    # FIRST match in document order, which is that template, not the article.
    main_content = soup.find('div', {'data-elementor-type': 'single-post'})
    if not main_content:
        main_content = soup.find('div', {'data-elementor-type': 'wp-page'})
    if not main_content:
        candidates = soup.find_all('div', {'data-elementor-type': 'wp-post'})
        for c in candidates:
            if c.get('data-elementor-post-type') not in ('elementskit_template', 'elementor_library'):
                main_content = c
                break
    if not main_content:
        main_content = soup.find('main')

    if main_content:
        main_html = str(main_content)
    else:
        print(f"COULD NOT FIND CONTENT: {slug}")
        sys.stdout.flush()
        main_html = "<h1>Could not find main content</h1>"

    # Extract Yoast meta title/description for reference (not injected, just kept as comment)
    title_tag = soup.find('title')
    page_title = title_tag.text.strip() if title_tag else slug

    head_styles = ""
    head = soup.find('head')
    if head:
        for link in head.find_all('link', rel='stylesheet'):
            href = link.get('href', '')
            if 'post-' in href or 'dynamic-content-for-elementor' in href or 'elementskit' in href or 'fluentform' in href:
                head_styles += str(link) + "\n"

    head_styles = re.sub(r'(\.css)\?[^"\']+', r'\1', head_styles)

    main_html = re.sub(r'data-dce-background-image-url=["\']([^"\']+)["\']', r'style="background-image: url(\1); background-size: cover; background-position: center; background-repeat: no-repeat;"', main_html)

    final_html = "<x-layouts.app>\n" + make_spa(head_styles) + "\n" + make_spa(main_html) + "\n</x-layouts.app>"

    with open(out_path, 'w', encoding='utf-8') as f:
        f.write(final_html)

    return True

success = 0
failed = []
for i, slug in enumerate(slugs):
    ok = scrape_article(slug)
    if ok:
        success += 1
    else:
        failed.append(slug)
    time.sleep(0.25)

print(f"\nDone. {success}/{len(slugs)} succeeded.")
if failed:
    print("Failed slugs:")
    for s in failed:
        print(" -", s)
