# /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:
    def __init__(self, category: str):
        self.category = category # car, bike, truck
        self.headers = {"User-Agent": "ServiceFinder-Harvester-Bot/2.0"}

    async def check_exists(self, db: AsyncSession, brand: str, model: str, gen: str = None):
        """Ellenőrzi a katalógusban való létezést."""
        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 az AssetCatalog-ban."""
        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"),
                engine_code=specs.get("engine_code")
            )
            db.add(new_v)
            logger.info(f"🆕 Új katalógus elem: {brand} {model}")
            return True
        return False