"""
Celery application for LabGraph.

Referenced by docker-compose as: celery -A config.celery <subcommand>

Queues:
  discovery — Proxmox API polling, nmap network scans
  heartbeat — Periodic ICMP/TCP liveness checks
  default   — Maintenance, cleanup, housekeeping
"""

import os

from celery import Celery
from celery.schedules import crontab
from kombu import Exchange, Queue

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.development")

app = Celery("labgraph")

app.config_from_object("django.conf:settings", namespace="CELERY")

app.autodiscover_tasks(["tasks"])

# Explicit queue definitions (mirrors --queues flag in docker-compose worker command)
app.conf.task_queues = (
    Queue("default", Exchange("default"), routing_key="default"),
    Queue("discovery", Exchange("discovery"), routing_key="discovery"),
    Queue("heartbeat", Exchange("heartbeat"), routing_key="heartbeat"),
)

app.conf.task_default_queue = "default"
app.conf.task_default_exchange = "default"
app.conf.task_default_routing_key = "default"

# Route tasks to queues by module prefix
app.conf.task_routes = {
    "tasks.discovery.*": {"queue": "discovery"},
    "tasks.heartbeat.*": {"queue": "heartbeat"},
    "tasks.maintenance.*": {"queue": "default"},
}

app.conf.beat_schedule = {
    "heartbeat-all-nodes": {
        "task": "tasks.heartbeat.check_all_nodes",
        "schedule": 60.0,
        "queue": "heartbeat",
        # Drop task if not picked up before the next run fires
        "options": {"expires": 55},
    },
    "discovery-scan-networks": {
        "task": "tasks.discovery.scan_all_networks",
        "schedule": crontab(minute="*/15"),
        "queue": "discovery",
    },
    "maintenance-prune-heartbeat-logs": {
        "task": "tasks.maintenance.prune_old_heartbeat_logs",
        "schedule": crontab(hour=3, minute=0),
        "queue": "default",
    },
}
