# app/tasks/generation_recovery_tasks.py
from __future__ import annotations

from typing import Any, Dict

from app.models.base import async_session
from app.tasks.async_runner import run_async
from app.tasks.celery_app import celery_app


async def _run_download_once() -> Dict[str, Any]:
    from app.services.generation_recovery_service import recover_download_tasks_once

    async with async_session() as db:
        return await recover_download_tasks_once(db)


async def _run_generation_once() -> Dict[str, Any]:
    from app.services.generation_recovery_service import recover_generation_tasks_once

    async with async_session() as db:
        return await recover_generation_tasks_once(db)


if celery_app:

    @celery_app.task(name="generation.recover_download_tasks_once")
    def recover_download_tasks_once() -> Dict[str, Any]:
        return run_async(_run_download_once())


    @celery_app.task(name="generation.recover_generation_tasks_once")
    def recover_generation_tasks_once() -> Dict[str, Any]:
        return run_async(_run_generation_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_download_tasks_once = _DisabledTask()
    recover_generation_tasks_once = _DisabledTask()
