{% if advanced_alchemy %}
from advanced_alchemy.extensions.litestar import (
    AsyncSessionConfig,
    SQLAlchemyAsyncConfig,
    SQLAlchemyPlugin,
)
{% endif %}
{% if litestar_vite %}
from pathlib import Path
from litestar_vite import ViteConfig, PathConfig
{% endif %}
{% if has_store %}
from litestar.stores.redis import RedisStore
{% endif %}
{% if litestar_saq %}
from litestar_saq import CronJob, QueueConfig, SAQConfig, SAQPlugin
{% endif %}

from src.backend.settings import get_settings

settings = get_settings()


{% if advanced_alchemy %}
alchemy_config = SQLAlchemyAsyncConfig(
    connection_string=settings.DATABASE_URL,
    before_send_handler="autocommit",
    session_config=AsyncSessionConfig(expire_on_commit=False),
    create_all=True,
)
alchemy = SQLAlchemyPlugin(config=alchemy_config)
{% endif %}

{% if litestar_vite %}
vite_config = ViteConfig(
    mode="spa",
    dev_mode=True,
    types=True,
    paths=PathConfig(
        root=Path(__file__).parent.parent.parent / "src" / "frontend",
        resource_dir="src",
        bundle_dir="public",
    ),
)
{% endif %}

{% if has_store %}
redis_store = RedisStore.with_client(
    url=settings.REDIS_URL,
    port=6379,
    db=0,
)
{% endif %}

{% if litestar_saq %}
saq = SAQPlugin(
    config=SAQConfig(
        use_server_lifespan=True,
        queue_configs=[
            QueueConfig(
                name="default",
                dsn=settings.REDIS_URL,
                scheduled_tasks=[
                    CronJob(
                        function="src.backend.lib.tasks.sample_task",
                        cron="* * * * *",
                        timeout=60,
                        ttl=2000,
                    ),
                ],
            ),
        ],
    )
)
{% endif %}
