from __future__ import annotations

from typing import Any

from app.models.base import async_session
from app.services.module_async_recovery_service import recover_module_async_tasks_once
from app.tasks.async_runner import run_async
from app.tasks.celery_app import celery_app


async def _run_recover_module_async_tasks_once() -> dict[str, Any]:
    async with async_session() as db:
        return await recover_module_async_tasks_once(db)


if celery_app:

    @celery_app.task(name="module_async.recover_module_async_tasks_once")
    def recover_module_async_tasks_once_task() -> dict[str, Any]:
        return run_async(_run_recover_module_async_tasks_once())

else:

    class _DisabledTask:
        def delay(self, *args: Any, **kwargs: Any) -> None:
            raise RuntimeError("Celery is disabled")

        def apply_async(self, *args: Any, **kwargs: Any) -> None:
            raise RuntimeError("Celery is disabled")

    recover_module_async_tasks_once_task = _DisabledTask()
