"""
Core app configuration
"""
from django.apps import AppConfig
import logging

logger = logging.getLogger(__name__)


class CoreConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'core'

    def ready(self):
        """
        Initialize APScheduler when Django starts
        """
        from core.scheduler import scheduler, start_scheduler
        from links.tasks import (
            schedule_pending_pages, schedule_pending_screenshots,
            retry_stuck_image_imports, flush_click_buffer,
        )
        from apscheduler.triggers.interval import IntervalTrigger

        # Start the scheduler
        start_scheduler()

        # Read intervals from SiteSettings (fall back to 120s if DB not ready)
        try:
            from links.models import SiteSettings
            ss = SiteSettings.get()
            pages_interval = ss.schedule_pending_pages_interval or 120
            screenshots_interval = ss.schedule_pending_screenshots_interval or 120
        except Exception:
            pages_interval = 120
            screenshots_interval = 120

        # Add periodic job for checking pending pages
        scheduler.add_job(
            schedule_pending_pages,
            trigger=IntervalTrigger(seconds=pages_interval),
            id='schedule_pending_pages',
            replace_existing=True,
        )
        logger.info(f"Scheduled periodic task: schedule_pending_pages (every {pages_interval}s)")

        # Add periodic job for recovering stuck screenshots
        scheduler.add_job(
            schedule_pending_screenshots,
            trigger=IntervalTrigger(seconds=screenshots_interval),
            id='schedule_pending_screenshots',
            replace_existing=True,
        )
        logger.info(f"Scheduled periodic task: schedule_pending_screenshots (every {screenshots_interval}s)")

        # Add periodic job for retrying stuck image imports (every 5 minutes)
        scheduler.add_job(
            retry_stuck_image_imports,
            trigger=IntervalTrigger(seconds=300),
            id='retry_stuck_image_imports',
            replace_existing=True,
        )
        logger.info("Scheduled periodic task: retry_stuck_image_imports (every 300s)")

        # Flush Redis-buffered click counts to the database (every 60 seconds)
        scheduler.add_job(
            flush_click_buffer,
            trigger=IntervalTrigger(seconds=60),
            id='flush_click_buffer',
            replace_existing=True,
        )
        logger.info("Scheduled periodic task: flush_click_buffer (every 60s)")

        # ── nginxmon: monthly geo DB refresh ──────────────────────────────────
        try:
            from nginxmon.geo import download_geo_db as refresh_geo_db
            scheduler.add_job(
                refresh_geo_db,
                trigger=IntervalTrigger(days=30),
                id='refresh_geo_db',
                replace_existing=True,
            )
            logger.info('nginxmon: scheduled geo DB refresh (every 30 days)')
        except Exception as exc:
            logger.warning('nginxmon: geo DB scheduler setup failed: %s', exc)

        # ── routermon ──────────────────────────────────────────────────────────
        try:
            from routermon.tasks import cleanup_old_queries
            from routermon.models import RouterMonSettings
            from routermon.receiver import start_receiver

            rm_settings = RouterMonSettings.get()
            if rm_settings.enabled:
                start_receiver(rm_settings.syslog_port)

            scheduler.add_job(
                cleanup_old_queries,
                trigger=IntervalTrigger(hours=6),
                id='routermon_cleanup',
                replace_existing=True,
            )
            logger.info("routermon: scheduled cleanup job (every 6h)")
        except Exception as exc:
            import sys
            print(f'routermon: startup error (non-fatal): {exc}', file=sys.stderr, flush=True)
            logger.warning("routermon: startup error (non-fatal): %s", exc)

        # ── invest: weekly portfolio snapshot (Saturday 08:00) ─────────────────
        try:
            from invest.tasks import snapshot_all_portfolios
            from apscheduler.triggers.cron import CronTrigger
            scheduler.add_job(
                snapshot_all_portfolios,
                CronTrigger(day_of_week='sat', hour=8, minute=0),
                id='weekly_portfolio_snapshot',
                replace_existing=True,
            )
            logger.info("invest: scheduled weekly portfolio snapshot (Saturday 08:00)")
        except Exception as exc:
            logger.warning("invest: snapshot scheduler setup failed: %s", exc)
