import httpx
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from app.models.vehicle import VehicleCatalog

class BaseHarvester:
    def __init__(self, category: str):
        self.category = category
        self.headers = {"User-Agent": "ServiceFinder-Harvester-Bot/2.0"}

    async def check_exists(self, db: AsyncSession, brand: str, model: str):
        """Ellenőrzi, hogy az adott modell létezik-e már."""
        stmt = select(VehicleCatalog).where(
            VehicleCatalog.brand == brand,
            VehicleCatalog.model == model,
            VehicleCatalog.category == self.category
        )
        result = await db.execute(stmt)
        return result.scalar_one_or_none()

    async def log_entry(self, db: AsyncSession, brand: str, model: str, specs: dict = None):
        """Létrehoz vagy frissít egy katalógus bejegyzést."""
        existing = await self.check_exists(db, brand, model)
        if not existing:
            new_v = VehicleCatalog(
                brand=brand,
                model=model,
                category=self.category,
                factory_specs=specs or {},
                verification_status="incomplete" if not specs else "verified"
            )
            db.add(new_v)
            return True
        return False