from celery import Celery
from celery.schedules import crontab
from app.config import settings

celery_app = Celery(
    "schaefflerautomat",
    broker=settings.redis_url,
    backend=settings.redis_url,
    include=[
        "app.tasks.step_tasks",  # shim — re-exports from domains.pipeline.tasks.*
        "app.domains.pipeline.tasks.extract_metadata",
        "app.domains.pipeline.tasks.render_thumbnail",
        "app.domains.pipeline.tasks.render_order_line",
        "app.domains.pipeline.tasks.export_glb",
        "app.tasks.ai_tasks",
        "app.tasks.beat_tasks",
        "app.domains.rendering.tasks",
        "app.domains.products.tasks",
        "app.domains.imports.tasks",
        "app.domains.materials.tasks",
        "app.tasks.gpu_tasks",
    ],
)

celery_app.conf.update(
    task_serializer="json",
    result_serializer="json",
    accept_content=["json"],
    timezone="UTC",
    enable_utc=True,
    task_routes={
        "app.domains.pipeline.tasks.*": {"queue": "step_processing"},
        "app.domains.rendering.tasks.*": {"queue": "asset_pipeline"},
        "app.tasks.beat_tasks.*": {"queue": "step_processing"},
        "app.tasks.ai_tasks.*": {"queue": "ai_validation"},
        # Legacy task names (shim) — keep until old queued tasks drain
        "app.tasks.step_tasks.*": {"queue": "step_processing"},
    },
    beat_schedule={
        "broadcast-queue-status-every-10s": {
            "task": "app.tasks.beat_tasks.broadcast_queue_status",
            "schedule": 10.0,  # every 10 seconds
        },
        "recover-stuck-cad-files-every-5m": {
            "task": "app.tasks.beat_tasks.recover_stuck_cad_files",
            "schedule": 300.0,  # every 5 minutes
        },
        "batch-render-notifications-every-60s": {
            "task": "app.tasks.beat_tasks.batch_render_notifications",
            "schedule": 60.0,  # every 60 seconds
        },
        "recover-stalled-renders-every-5m": {
            "task": "app.tasks.beat_tasks.recover_stalled_renders",
            "schedule": 300.0,  # every 5 minutes
        },
        "apply-worker-concurrency-every-5m": {
            "task": "app.tasks.beat_tasks.apply_worker_concurrency",
            "schedule": 300.0,  # every 5 minutes
        },
        "probe-gpu-every-30m": {
            "task": "app.tasks.gpu_tasks.probe_gpu",
            "schedule": 1800.0,  # every 30 minutes
        },
    },
)
