from datetime import datetime, timedelta
from sqlalchemy import select
from app.models.user import User
from app.models.vehicle import Vehicle
from app.core.email import send_expiry_notification

async def check_expiring_documents(db: AsyncSession, background_tasks: BackgroundTasks):
    # Példa: Műszaki vizsga lejárata 30 napon belül
    threshold = datetime.now().date() + timedelta(days=30)
    result = await db.execute(
        select(Vehicle, User).join(User).where(Vehicle.mot_expiry_date <= threshold)
    )
    for vehicle, user in result.all():
        send_expiry_notification(background_tasks, user.email, f"Műszaki vizsga ({vehicle.license_plate})")