# /opt/docker/dev/service_finder/backend/app/services/harvester_base.py
import httpx
import logging
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from app.models.asset import AssetCatalog

logger = logging.getLogger(__name__)

class BaseHarvester:
    """ MDM Adatgyűjtő Alaposztály. """
    def __init__(self, category: str):
        self.category = category # 'car', 'motorcycle', 'truck'
        self.headers = {"User-Agent": "ServiceFinder-Harvester-Bot/2.1"}

    async def check_exists(self, db: AsyncSession, brand: str, model: str, gen: str = None):
        """ Ellenőrzi a katalógusban való létezést az új AssetCatalog modellben. """
        stmt = select(AssetCatalog).where(
            AssetCatalog.make == brand,
            AssetCatalog.model == model,
            AssetCatalog.vehicle_class == self.category
        )
        if gen:
            stmt = stmt.where(AssetCatalog.generation == gen)
            
        result = await db.execute(stmt)
        return result.scalar_one_or_none()

    async def log_entry(self, db: AsyncSession, brand: str, model: str, specs: dict):
        """ Létrehoz vagy frissít egy bejegyzést. Támogatja a factory_data dúsítást. """
        existing = await self.check_exists(db, brand, model, specs.get("generation"))
        if not existing:
            new_v = AssetCatalog(
                make=brand,
                model=model,
                generation=specs.get("generation"),
                year_from=specs.get("year_from"),
                year_to=specs.get("year_to"),
                vehicle_class=self.category,
                fuel_type=specs.get("fuel_type"),
                power_kw=specs.get("power_kw"),
                engine_capacity=specs.get("engine_capacity"),
                factory_data=specs.get("factory_data", {}) # MDM JSONB tárolás
            )
            db.add(new_v)
            logger.info(f"🆕 Új katalógus elem rögzítve: {brand} {model}")
            return True
        return False