from datetime import timedelta

from evibes.settings.base import REDIS_PASSWORD, TIME_ZONE

CELERY_ENABLE_UTC = False
CELERY_TIMEZONE = TIME_ZONE

CELERY_BROKER_URL = f"redis://:{REDIS_PASSWORD}@redis:6379/0"

CELERY_BROKER_CONNECTION_RETRY_ON_STARTUP = True
CELERY_BROKER_HEARTBEAT = 10
CELERY_BROKER_HEARTBEAT_CHECKRATE = 2
CELERY_BROKER_POOL_LIMIT = 10

CELERY_BROKER_TRANSPORT_OPTIONS = {
    "visibility_timeout": 3600,
    "retry_policy": {
        "interval_start": 0.1,
        "interval_step": 0.2,
        "max_retries": 5,
    },
    "socket_keepalive": True,
    "socket_timeout": 30,
    "socket_connect_timeout": 30,
    "retry_on_timeout": True,
}

CELERY_RESULT_BACKEND = CELERY_BROKER_URL

CELERY_RESULT_EXTENDED = True
CELERY_RESULT_EXPIRES = timedelta(days=1)
CELERY_RESULT_COMPRESSION = "zlib"

CELERY_TASK_SERIALIZER = "orjson"
CELERY_RESULT_SERIALIZER = "orjson"
CELERY_ACCEPT_CONTENT = ["orjson", "json"]
CELERY_EVENT_SERIALIZER = "orjson"

CELERY_TASK_ACKS_LATE = True
CELERY_TASK_REJECT_ON_WORKER_LOST = True

CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_SEND_SENT_EVENT = True
CELERY_WORKER_SEND_TASK_EVENTS = True

CELERY_TASK_SOFT_TIME_LIMIT = 3600
CELERY_TASK_TIME_LIMIT = 3900

CELERY_TASK_ANNOTATIONS = {
    "engine.core.tasks.update_products_task": {
        "rate_limit": "1/m",
        "time_limit": 7200,
        "soft_time_limit": 6900,
        "acks_late": True,
        "reject_on_worker_lost": True,
    },
}

CELERY_WORKER_HIJACK_ROOT_LOGGER = False

CELERY_WORKER_PREFETCH_MULTIPLIER = 4

CELERY_WORKER_MAX_TASKS_PER_CHILD = 1000
CELERY_WORKER_DISABLE_RATE_LIMITS = False

CELERY_TASK_ROUTES = {
    "engine.core.tasks.update_products_task": {"queue": "stock_updater"},
}

CELERY_TASK_DEFAULT_QUEUE = "default"
CELERY_TASK_DEFAULT_EXCHANGE = "default"
CELERY_TASK_DEFAULT_ROUTING_KEY = "default"

CELERY_TASK_STORE_ERRORS_EVEN_IF_IGNORED = True

CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers.DatabaseScheduler"

CELERY_BEAT_SCHEDULE = {
    "backup_task": {
        "task": "engine.core.tasks.backup_task",
        "schedule": timedelta(days=7),
        "options": {"queue": "default"},
    },
    "update_products_task": {
        "task": "engine.core.tasks.update_products_task",
        "schedule": timedelta(minutes=60),
        "options": {"queue": "stock_updater"},
    },
    "update_orderproducts_task": {
        "task": "engine.core.tasks.update_orderproducts_task",
        "schedule": timedelta(minutes=1),
        "options": {"queue": "default"},
    },
    "set_default_caches_task": {
        "task": "engine.core.tasks.set_default_caches_task",
        "schedule": timedelta(hours=4),
        "options": {"queue": "default"},
    },
    "remove_stale_product_images": {
        "task": "engine.core.tasks.remove_stale_product_images",
        "schedule": timedelta(days=1),
        "options": {"queue": "default"},
    },
    "process_promotions": {
        "task": "engine.core.tasks.process_promotions",
        "schedule": timedelta(hours=2),
        "options": {"queue": "default"},
    },
}
