"""Tortoise ORM connection setup.

Uses ``Path(__file__)``-relative paths (no CWD reliance) so the app is
portable regardless of the working directory it is launched from.
"""

from __future__ import annotations

from pathlib import Path

from tortoise import Tortoise

from {{ cookiecutter.project_name }}.config.settings import settings

_MODELS_PATH = "{{ cookiecutter.project_name }}.db.models"


async def init_db() -> None:
    """Initialize Tortoise with generate_schemas (built-in, NOT Aerich)."""
    await Tortoise.init(
        db_url=settings.database.url,
        modules={"models": [_MODELS_PATH]},
    )
    await Tortoise.generate_schemas(safe=True)


async def close_db() -> None:
    """Close all Tortoise connections."""
    await Tortoise.close_connections()