from fastapi import APIRouter, Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, or_
from app.db.session import get_db
from app.models.vehicle import VehicleCatalog
from typing import List

router = APIRouter()

@router.get("/search")
async def search_catalog(
    q: str = Query(..., min_length=2, description="Márka vagy típus keresése"),
    category: str = None,
    db: AsyncSession = Depends(get_db)
):
    """Keresés a Robot által feltöltött katalógusban."""
    stmt = select(VehicleCatalog).where(
        or_(
            VehicleCatalog.brand.ilike(f"%{q}%"),
            VehicleCatalog.model.ilike(f"%{q}%")
        )
    )
    if category:
        stmt = stmt.where(VehicleCatalog.category == category)
    
    result = await db.execute(stmt.limit(20))
    items = result.scalars().all()
    
    return [
        {
            "id": i.id,
            "full_name": f"{i.brand} {i.model}",
            "category": i.category,
            "status": i.verification_status,
            "specs": i.factory_specs
        } for i in items
    ]