"""
Scheduler module for handling background tasks
"""
import logging
from apscheduler.schedulers.background import BackgroundScheduler
import config
from utils.backup import create_backup


def scheduled_backup():
    """Führt ein geplantes Backup aus"""
    logging.info("Starte geplantes Backup...")
    create_backup(backup_type="scheduled", created_by="scheduler")


def init_scheduler():
    """Initialize and configure the scheduler"""
    scheduler = BackgroundScheduler()
    
    # Configure daily backup job
    scheduler.add_job(
        scheduled_backup,
        'cron',
        hour=config.SCHEDULER_CONFIG['backup_hour'],
        minute=config.SCHEDULER_CONFIG['backup_minute'],
        id='daily_backup',
        replace_existing=True
    )
    
    scheduler.start()
    logging.info(f"Scheduler started. Daily backup scheduled at {config.SCHEDULER_CONFIG['backup_hour']:02d}:{config.SCHEDULER_CONFIG['backup_minute']:02d}")
    
    return scheduler