from __future__ import annotations

from typing import Any
from typing import cast

from hypothesis.strategies import DrawFn
from hypothesis.strategies import composite
from sqlalchemy import Engine
from sqlalchemy import MetaData

from utilities.sqlalchemy import create_engine

from .hypothesis import temp_paths  # noqa: TID252


@composite
def sqlite_engines(
    _draw: DrawFn, /, *, metadata: MetaData | None = None, base: Any = None
) -> Engine:
    """Strategy for generating SQLite engines."""
    temp_path = _draw(temp_paths())
    path = temp_path.joinpath("db.sqlite")
    engine = create_engine("sqlite", database=path.as_posix())
    if metadata is not None:
        metadata.create_all(engine)
    if base is not None:
        base.metadata.create_all(engine)

    # attach temp_path to the engine, so as to keep it alive
    cast(Any, engine).temp_path = temp_path

    return engine
