"""SqlCatalog factory backed by a local SQLite file."""

from __future__ import annotations

from pathlib import Path

from pyiceberg.catalog.sql import SqlCatalog

WAREHOUSE = (Path(__file__).resolve().parent.parent / "warehouse").as_posix()


def open_catalog() -> SqlCatalog:
    """Return a SqlCatalog backed by SQLite under sample-pipeline/warehouse/.

    Idempotent -- the catalog initialises its tables on first use.
    """
    Path(WAREHOUSE).mkdir(parents=True, exist_ok=True)
    return SqlCatalog(
        "default",
        **{
            "uri": f"sqlite:///{WAREHOUSE}/catalog.db",
            "warehouse": f"file://{WAREHOUSE}",
        },
    )
