# /opt/docker/dev/service_finder/backend/app/api/v1/endpoints/evidence.py
from fastapi import APIRouter, UploadFile, File, HTTPException, status, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, func, text
from app.api.deps import get_db, get_current_user
from app.models.identity import User
from app.models import Asset # JAVÍTVA: Asset modell

router = APIRouter()

@router.post("/scan-registration")
async def scan_registration_document(file: UploadFile = File(...), db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user)):
    stmt_limit = text("SELECT (value->>:plan)::int FROM system.system_parameters WHERE key = 'VEHICLE_LIMIT'")
    res = await db.execute(stmt_limit, {"plan": current_user.subscription_plan or "free"})
    max_allowed = res.scalar() or 1

    stmt_count = select(func.count(Asset.id)).where(Asset.owner_organization_id == current_user.scope_id)
    count = (await db.execute(stmt_count)).scalar() or 0
    
    if count >= max_allowed:
        raise HTTPException(status_code=403, detail=f"Limit túllépés: {max_allowed} jármű engedélyezett.")

    # OCR hívás helye...
    return {"success": True, "message": "Feldolgozás megkezdődött."}