from apscheduler.schedulers.asyncio import AsyncIOScheduler
from datetime import datetime, timedelta
from backend.core.scanner import run_schema_scan, run_image_scan

scheduler = AsyncIOScheduler()

async def scheduled_schema():
    await run_schema_scan()

async def scheduled_image():
    await run_image_scan()

def start_scheduler():
    # Schema checks run fast, do it every 1 hour. Immediate start.
    scheduler.add_job(scheduled_schema, 'date', run_date=datetime.now() + timedelta(seconds=2))
    scheduler.add_job(scheduled_schema, 'interval', hours=1)
    
    # Image checks are heavy, do it every 3 hours. Start slightly after schema.
    scheduler.add_job(scheduled_image, 'date', run_date=datetime.now() + timedelta(seconds=10))
    scheduler.add_job(scheduled_image, 'interval', hours=3)
    
    scheduler.start()
    print("Background schedulers started (Schema: 1hr, Image: 3hrs).")
