from fastapi import APIRouter, Depends, UploadFile, File, BackgroundTasks, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.session import get_db
from app.services.document_service import DocumentService

router = APIRouter()

@router.post("/upload/{parent_type}/{parent_id}")
async def upload_document(
    parent_type: str,
    parent_id: str,
    background_tasks: BackgroundTasks,
    file: UploadFile = File(...),
    db: AsyncSession = Depends(get_db)
):
    """
    Dokumentum feltöltés, optimalizálás és NAS-ra mentés.
    parent_type: 'organizations' vagy 'assets'
    """
    if parent_type not in ["organizations", "assets"]:
        raise HTTPException(status_code=400, detail="Érvénytelen cél-típus!")
        
    doc = await DocumentService.process_upload(file, parent_type, parent_id, db, background_tasks)
    
    return {
        "document_id": doc.id,
        "thumbnail": doc.thumbnail_path,
        "original_name": doc.original_name,
        "status": "processed_and_vaulted"
    }