import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

def fetch_page(url):
    resp = requests.get(url, timeout=10)
    resp.raise_for_status()
    return resp.text

def analyse_seo(url):
    """
    Analyse une page PrestaShop (ou autre) :
    - titre
    - meta description
    - canonical
    - meta robots
    - og:title
    - og:description
    - og:image
    """
    html = fetch_page(url)
    soup = BeautifulSoup(html, 'html.parser')
    title = soup.title.string.strip() if soup.title else ''
    meta_desc = ''
    canonical = ''
    meta_robots = ''
    og_title = ''
    og_desc = ''
    og_image = ''
    desc_tag = soup.find('meta', attrs={'name': 'description'})
    if desc_tag and desc_tag.get('content'):
        meta_desc = desc_tag['content']
    canonical_tag = soup.find('link', rel='canonical')
    if canonical_tag and canonical_tag.get('href'):
        canonical = canonical_tag['href']
    robots_tag = soup.find('meta', attrs={'name': 'robots'})
    if robots_tag and robots_tag.get('content'):
        meta_robots = robots_tag['content']
    og_title_tag = soup.find('meta', property='og:title')
    if og_title_tag and og_title_tag.get('content'):
        og_title = og_title_tag['content']
    og_desc_tag = soup.find('meta', property='og:description')
    if og_desc_tag and og_desc_tag.get('content'):
        og_desc = og_desc_tag['content']
    og_image_tag = soup.find('meta', property='og:image')
    if og_image_tag and og_image_tag.get('content'):
        og_image = og_image_tag['content']
    return {
        'url': url,
        'title': title,
        'meta_description': meta_desc,
        'canonical': canonical,
        'meta_robots': meta_robots,
        'og_title': og_title,
        'og_description': og_desc,
        'og_image': og_image
    }
