#!/usr/bin/env python3
import asyncio
import logging
import random
import json
import re
from bs4 import BeautifulSoup
from playwright.async_api import async_playwright
from sqlalchemy import text
from app.database import AsyncSessionLocal

logging.basicConfig(level=logging.INFO, format='%(asctime)s [R2-MASTER] %(message)s')
logger = logging.getLogger("R2-AutoData")

class AutoDataMaster:
    def __init__(self):
        self.base_url = "https://www.auto-data.net"

    def clean_key(self, key):
        if "," in key: key = key.split(",")[-1]
        key = key.replace("What is the ", "").replace("How much ", "").replace("How many ", "")
        key = key.split("?")[0].strip()
        return key.capitalize()

    async def get_soup(self, page, url):
        delay = random.uniform(2, 5)
        await asyncio.sleep(delay)
        # JAVÍTÁS: Megvárjuk, amíg a hálózat elcsendesedik (biztosabb betöltés)
        await page.goto(url, wait_until="networkidle", timeout=60000)
        content = await page.content()
        return BeautifulSoup(content, 'html.parser')

    async def scrape_engine_details(self, page, url):
        try:
            soup = await self.get_soup(page, url)
            data = {
                "make": "", "model": "", "generation": "", "modification": "",
                "year_from": None, "year_to": None, "power_kw": 0, "engine_cc": 0,
                "specifications": {}, "source_url": url
            }
            # (Az adatkinyerő logika ugyanaz marad, mint az előzőleg sikeresen tesztelt Honda esetén)
            rows = soup.find_all('tr')
            for row in rows:
                th, td = row.find('th'), row.find('td')
                if not th or not td: continue
                raw_k, val = th.get_text(strip=True), td.get_text(strip=True)
                k_low = raw_k.lower()
                if "brand" == k_low: data["make"] = val
                elif "model" == k_low: data["model"] = val
                elif "generation" == k_low: data["generation"] = val
                elif "modification" == k_low: data["modification"] = val
                elif "start of production" in k_low:
                    m = re.search(r'(\d{4})', val); 
                    if m: data["year_from"] = int(m.group(1))
                elif "end of production" in k_low:
                    m = re.search(r'(\d{4})', val); 
                    if m: data["year_to"] = int(m.group(1))
                elif "power" == k_low:
                    hp_m = re.search(r'(\d+)\s*Hp', val, re.I)
                    if hp_m: data["power_kw"] = int(int(hp_m.group(1)) / 1.36)
                elif "displacement" in k_low:
                    cc_m = re.search(r'(\d+)\s*cm3', val)
                    if cc_m: data["engine_cc"] = int(cc_m.group(1))
                clean_k = self.clean_key(raw_k)
                if clean_k and val: data["specifications"][clean_k] = val
            return data
        except Exception as e:
            logger.error(f"Hiba az adatlapon ({url}): {e}")
            return None

    async def save_to_db(self, data):
        if not data or not data["make"]: return
        async with AsyncSessionLocal() as db:
            try:
                await db.execute(text("""
                    INSERT INTO vehicle.external_reference_library 
                    (source_name, make, model, generation, modification, year_from, year_to, power_kw, engine_cc, specifications, source_url)
                    VALUES ('auto-data.net', :make, :model, :gen, :mod, :y_f, :y_t, :p_kw, :e_cc, :specs, :url)
                    ON CONFLICT (source_url) DO UPDATE SET specifications = EXCLUDED.specifications, last_scraped_at = NOW();
                """), {
                    "make": data["make"], "model": data["model"], "gen": data["generation"],
                    "mod": data["modification"], "y_f": data["year_from"], "y_t": data["year_to"],
                    "p_kw": data["power_kw"], "e_cc": data["engine_cc"],
                    "specs": json.dumps(data["specifications"]), "url": data["source_url"]
                })
                await db.commit()
                logger.info(f"✅ MENTVE: {data['make']} {data['model']} {data['modification']}")
            except Exception as e:
                logger.error(f"DB Hiba: {e}")

    async def crawl(self):
        logger.info("🚀 Porszívózás indul...")
        async with async_playwright() as p:
            # Lassított indítás és normális ablakméret a lebukás ellen
            browser = await p.chromium.launch(headless=True)
            context = await browser.new_context(
                user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
                viewport={'width': 1920, 'height': 1080}
            )
            page = await context.new_page()

            # 1. MÁRKÁK LISTÁJA - JAVÍTOTT SZELEKTOR
            logger.info(f"Szint 1: Márkák betöltése...")
            soup = await self.get_soup(page, f"{self.base_url}/en/allbrands")
            
            # Az auto-data-n a márkák linkjeinek class-ja 'brandi' vagy 'brand'
            brand_elements = soup.select('a.brandi') or soup.select('a.brand')
            brand_links = []
            for a in brand_elements:
                href = a.get('href')
                if href and 'brand' in href:
                    full_url = href if href.startswith('http') else f"{self.base_url}/{href.lstrip('/')}"
                    brand_links.append(full_url)
            
            if not brand_links:
                logger.error(f"❌ 0 márkát találtam! Oldalcím: {soup.title.string if soup.title else 'Nincs'}")
                # Debug: mentsük el a HTML elejét, hogy lássuk mi az
                logger.info(f"HTML debug (első 500 karakter): {str(soup)[:500]}")
                await browser.close()
                return

            logger.info(f"🎯 Talált márkák: {len(brand_links)}")

            # Csak az első 3 márkát nézzük meg tesztként (Abarth, Acura, Alfa Romeo)
            for b_link in brand_links:
                try:
                    logger.info(f"Szint 2: Modellek keresése itt: {b_link}")
                    soup = await self.get_soup(page, b_link)
                    # Modellek szelektor: a.modeli
                    model_links = [self.base_url + '/' + a['href'].lstrip('/') for a in soup.select('a.modeli')]
                    
                    logger.info(f"  -> {len(model_links)} modellt találtam.")

                    for m_link in model_links:
                        logger.info(f"Szint 3: Generációk itt: {m_link}")
                        soup = await self.get_soup(page, m_link)
                        # Generációk szelektor: a.generation
                        gen_links = [self.base_url + '/' + a['href'].lstrip('/') for a in soup.select('a.generation')]
                        
                        for g_link in gen_links:
                            logger.info(f"Szint 4: Motorváltozatok itt: {g_link}")
                            soup = await self.get_soup(page, g_link)
                            # Motorváltozatok szelektor: a.car_specs
                            engine_links = [self.base_url + '/' + a['href'].lstrip('/') for a in soup.select('a.car_specs')]
                            
                            for e_link in engine_links:
                                data = await self.scrape_engine_details(page, e_link)
                                if data:
                                    await self.save_to_db(data)
                except Exception as e:
                    logger.error(f"Hiba a folyamatban: {e}")

            await browser.close()

if __name__ == "__main__":
    asyncio.run(AutoDataMaster().crawl())