from typing import Any, AsyncIterator

from fastapi import Request
from fastapi.params import Depends
from typing_extensions import Annotated

from imbue.contexts.task import TaskContainer
from imbue.dependency import Interface


async def _task_container(request: Request) -> AsyncIterator[TaskContainer]:
    """Initialize the task container.
    This needs to be done for every request.
    For more details, see:
        - https://fastapi.tiangolo.com/tutorial/dependencies/global-dependencies/
        - https://fastapi.tiangolo.com/tutorial/dependencies/
    """
    async with request.state.app_container.task_context() as container:
        yield container


request_lifespan = Depends(_task_container, use_cache=True)


class Dependency(Depends):
    def __init__(self, interface: Interface):
        async def _get(container: Annotated[TaskContainer, request_lifespan]) -> Any:
            return await container.get(interface)

        super().__init__(_get, use_cache=False)
