"""
Celery application for the Konstruct Agent Orchestrator.

Broker and result backend are both Redis (separate DB indexes to avoid
key collisions). Tasks are discovered automatically from orchestrator.tasks.

Usage (development):
    celery -A orchestrator.main worker --loglevel=info

Usage (production — via Docker Compose):
    celery -A orchestrator.main worker --loglevel=info --concurrency=4
"""

from __future__ import annotations

from celery import Celery

from shared.config import settings

app = Celery(
    "konstruct_orchestrator",
    broker=settings.celery_broker_url,
    backend=settings.celery_result_backend,
    include=["orchestrator.tasks"],
)

# ---------------------------------------------------------------------------
# Celery configuration
# ---------------------------------------------------------------------------
app.conf.update(
    task_serializer="json",
    accept_content=["json"],
    result_serializer="json",
    timezone="UTC",
    enable_utc=True,
    # Acknowledge tasks only after they complete (not on receipt)
    # This ensures tasks are retried if the worker crashes mid-execution.
    task_acks_late=True,
    # Reject tasks that exceed 10 minutes — prevents runaway LLM calls
    task_soft_time_limit=540,
    task_time_limit=600,
)
