import logging
from apscheduler.triggers.interval import IntervalTrigger
from core.scheduler import scheduler
from netscan.scanner import run_scan

logger = logging.getLogger(__name__)


def schedule_profile(profile):
    job_id = f'netscan_profile_{profile.pk}'
    scheduler.add_job(
        run_scan,
        trigger=IntervalTrigger(days=profile.schedule_interval),
        id=job_id,
        args=[profile.pk, 'scheduler'],
        replace_existing=True,
    )
    logger.info(f'Scheduled netscan job {job_id} every {profile.schedule_interval} day(s)')


def unschedule_profile(profile):
    job_id = f'netscan_profile_{profile.pk}'
    if scheduler.get_job(job_id):
        scheduler.remove_job(job_id)
        logger.info(f'Removed netscan job {job_id}')
