# /opt/docker/dev/service_finder/backend/app/services/harvester_cars.py
import httpx
import asyncio
from sqlalchemy.ext.asyncio import AsyncSession
from .harvester_base import BaseHarvester

class VehicleHarvester(BaseHarvester):
    def __init__(self):
        super().__init__(category="car")
        self.base_url = "https://www.carqueryapi.com/api/0.3/"

    async def _get_api_data(self, params: dict):
        async with httpx.AsyncClient() as client:
            try:
                response = await client.get(self.base_url, params=params, headers=self.headers, timeout=15.0)
                if response.status_code == 200:
                    text = response.text
                    if text.startswith("?("): text = text[2:-2]
                    return response.json()
                return None
            except Exception as e:
                print(f"CarQuery Robot Hiba: {e}")
                return None

    async def harvest_all(self, db: AsyncSession):
        """ Automatikus CarQuery szinkronizáció MDM alapon. """
        print("🚗 Személyautó Robot: Indul az adatgyűjtés...")
        
        makes_data = await self._get_api_data({"cmd": "getMakes", "sold_in_us": 0})
        if not makes_data: return
        
        for make in makes_data.get("Makes", [])[:50]: # Teszt limit
            make_id = make['make_id']
            make_name = make['make_display']
            
            models_data = await self._get_api_data({"cmd": "getModels", "make": make_id})
            if not models_data: continue
            
            for model in models_data.get("Models", []):
                specs = {
                    "factory_data": {"api_source": "carquery", "api_make_id": make_id}
                }
                await self.log_entry(db, make_name, model['model_name'], specs)
            
            await db.commit()
            await asyncio.sleep(1) # Rate limiting
        
        print("🏁 Személyautó Robot: Adatok szinkronizálva.")